- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是拥有一个 Store
其中包含各种 Entry
项目。 Entry
元素可以比商店本身生命周期更长,所以 Store
保持 Vec<&Entry>
( playground ):
struct Entry {
title: String,
content: String,
}
struct Store<'a> {
// name: String,
entries: Vec<&'a Entry>,
}
impl<'a> Store<'a> {
fn new() -> Store<'a> {
Store {
// name,
entries: Vec::new(),
}
}
fn add_entry(self: &mut Store, entry: &Entry) {
self.entries.push(entry);
}
}
fn main() {
let entry = Entry {
title: "my title",
content: "my content",
};
let mut store = Store::new();
store.add_entry(entry);
}
error[E0308]: mismatched `self` parameter type
--> src/main.rs:19:24
|
19 | fn add_entry(self: &mut Store, entry: &Entry) {
| ^^^^^^^^^^ lifetime mismatch
|
= note: expected struct `Store<'a>`
found struct `Store<'_>`
note: the anonymous lifetime #2 defined on the method body at 19:5...
--> src/main.rs:19:5
|
19 | fn add_entry(self: &mut Store, entry: &Entry) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 11:6
--> src/main.rs:11:6
|
11 | impl<'a> Store<'a> {
| ^^
error[E0308]: mismatched `self` parameter type
--> src/main.rs:19:24
|
19 | fn add_entry(self: &mut Store, entry: &Entry) {
| ^^^^^^^^^^ lifetime mismatch
|
= note: expected struct `Store<'a>`
found struct `Store<'_>`
note: the lifetime `'a` as defined on the impl at 11:6...
--> src/main.rs:11:6
|
11 | impl<'a> Store<'a> {
| ^^
note: ...does not necessarily outlive the anonymous lifetime #2 defined on the method body at 19:5
--> src/main.rs:19:5
|
19 | fn add_entry(self: &mut Store, entry: &Entry) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error E0308在这种情况下不是很有帮助。
我不明白为什么生命周期在 add_entry
方法必须长于 impl
之一,这是我从错误中了解到的。
我确信这是非常基础的东西,但我无法通过阅读 The Rust Programming Language 直到第 15 章来理解它。
最佳答案
修复错误使其编译:add_entry
应该如下所示:
fn add_entry(&mut self, entry: &'a Entry) {
self.entries.push(entry);
}
即您必须明确指定 entry
的生命周期。您的 Store
假定其 Entry
具有一定的生命周期。如果 add_entry
没有显式存储此生命周期,Rust 会尝试推断自动生命周期 - 它无法证明 entry
的生命周期实际上是您的 Store
要求。
如果你像以前一样离开 add_entry
,你基本上可以像这样调用它 store.add_entry(&Entry{...})
(即你可以传递一个引用到一个临时条目)。这个临时条目将超出范围/在声明之后。因此,Rust 不允许将它放入 entries
中,因为它会指向一个已经删除(读取:无效)的值。
关于rust - "does not necessarily outlive the lifetime"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66244743/
我的目标是拥有一个 Store其中包含各种 Entry项目。 Entry元素可以比商店本身生命周期更长,所以 Store保持 Vec ( playground ): struct Entry {
基于文档: Compiled code to be executed by the Java virtual machine is represented using a hardware- and
http://dev.mysql.com/doc/refman/5.5/en/innodb-consistent-read.html Note The snapshot of the database
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭10 年前。 Improv
我有以下代码试图最小化对数似然函数。 #!/usr/bin/python import math import random import numpy as np from scipy.optimiz
我喜欢 JavaFX 和 SceneBuilder,但是当它们不在同一个文件夹中时,我就是不知道如何让 SceneBuilder 将我的 FXML View 与其 Java Controller 链接
我是一名优秀的程序员,十分优秀!