- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在经历传说中的RFC 1214似乎我遗漏了一些重要的东西。
struct Foo;
struct Bar<'a> {
foo: &'a Foo
}
fn f<'x, 'y>(_: &'x Foo, _: &'y Bar<'x>)
where 'y: 'x, 'x: 'y {
}
fn g<'x>(x: &'x Foo) {
let y = Bar {foo : x};
f(x, &y); // ?
}
fn main(){
let x = Foo;
g(&x);
}
在这段代码中,我竭尽全力确保 'x : 'y
和 不是 'y : 'x
。定义x
的函数调用定义y
的函数,我相信这已经足以保证x
比y
,但我还在y
中放置了对x
的引用,只是为了确定。
现在,f
中的约束使得此函数的调用不可能有效。我的意思是,它可以,当且仅当 'x == 'y
时,但它看起来完全像 x
比 y
活得更久>,因为它是在外部范围中定义的。
然而,此代码会进行类型检查和编译。这怎么可能?
最佳答案
一生有variance ,也就是说,编译器可以选择缩短 &'a Foo
的生命周期给一些&'b Foo
.像这样的引用的生命周期就意味着 Foo
持续时间至少与 'a
一样长: 较短的生命周期仍然满足此保证。这就是这里发生的事情:'x
正在缩短生命周期以与 &y
具有相同的生命周期引用。
你可以使用不变性来停止这个编译:如果生命周期'x
不能缩短,那么代码将按预期停止编译。
use std::cell::Cell;
struct Foo;
struct Bar<'a> {
foo: Cell<&'a Foo>
}
fn f<'x, 'y>(_: Cell<&'x Foo>, _: &'y Bar<'x>)
where 'y: 'x, 'x: 'y {
}
fn g<'x>(x: Cell<&'x Foo>) {
let y = Bar {foo : x.clone()};
f(x, &y); // ?
}
fn main(){
let x = Foo;
g(Cell::new(&x));
}
<anon>:16:10: 16:11 error: `y` does not live long enough
<anon>:16 f(x, &y); // ?
^
<anon>:14:28: 17:2 note: reference must be valid for the lifetime 'x as defined on the block at 14:27...
<anon>:14 fn g<'x>(x: Cell<&'x Foo>) {
<anon>:15 let y = Bar {foo : x.clone()};
<anon>:16 f(x, &y); // ?
<anon>:17 }
<anon>:15:34: 17:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 15:33
<anon>:15 let y = Bar {foo : x.clone()};
<anon>:16 f(x, &y); // ?
<anon>:17 }
这里发生的是Cell<T>
在 T
中不变, 因为它是可读可写的。这特别意味着 Cell<&'x Foo>
不能缩短为 Cell<&'y Foo>
: 用引用填充它 &'y Foo
那是真的'y
(即只持续 'y
)将意味着一旦单元格离开 'y
引用就会悬空(但仍在 'x
中)。
关于rust - “outlives” 关系和实际范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050159/
我正在经历传说中的RFC 1214似乎我遗漏了一些重要的东西。 struct Foo; struct Bar { foo: &'a Foo } fn f(_: &'x Foo, _: &'y B
我的目标是拥有一个 Store其中包含各种 Entry项目。 Entry元素可以比商店本身生命周期更长,所以 Store保持 Vec ( playground ): struct Entry {
我试图将函数作为参数传递并在线程内调用它。这是我要运行的代码: use std::thread; pub struct Image { pub data: Vec, } pub trait I
我用 gtk-rs 构建了一个 GTK 应用程序。当我构建主窗口时,我想使用一些动态参数,例如窗口高度。我创建了一个包含所有此类设置的结构,并希望将其用作构建 UI 的函数的输入参数: fn main
我用 gtk-rs 构建了一个 GTK 应用程序。当我构建主窗口时,我想使用一些动态参数,例如窗口高度。我创建了一个包含所有此类设置的结构,并希望将其用作构建 UI 的函数的输入参数: fn main
我写了以下代码来测试闭包: fn main() { let captured_val = "a captured value".to_string(); let closure_par
我的 lib.rs 中有以下结构 pub enum ConfigurationSource { StringContent(String), FileContent(PathBuf)
当我尝试编译以下代码时: fn main() { (...) let mut should_end = false; let mut input = Input::new(ct
我正在尝试编写一个函数来转换以下形式的数据结构: input = [("a", [1,2,3]), ("b", [4,5,6])] 进入 output = [(a,1), (c,2) ..... (b
我的程序使用变量的内存地址作为唯一标识符。我知道这非常丑陋,但它是一种获取唯一标识符的非常轻量级的方法。 此模式仅在我将这些变量设为静态时有效,这样它们的唯一 ID(即地址)“永远”存在这意味着我有几
我有两个向量 itertools::MinMaxResult .我需要遍历第一个向量,对于每个元素,遍历第二个向量,检查第一个向量的最小值是否等于第二个向量的任何元素的最大值,反之亦然。这是我尝试过的
在下面的示例中,我有通过命令行参数设置的参数 verbose 和 data_source。 verbose 是一个 bool 标志,但 data_source 用于从一组可用函数中选择一个默认值,这些
我对 Rust 的了解还不够深,无法理解生命周期和闭包... 尝试使用 tokio-curl 将下载的数据收集到向量中: extern crate curl; extern crate futures
我想创建一个可以访问参数instanceContext的回调函数,但是我不确定如何修复该代码。 失败的代码是instanceContext.instances.len()。 我已经尝试过使用生命周期,
我想实现一个类似于调试 builders 的构建器由标准库定义。它们使用如下结构定义: struct DebugFoo { fmt: &'a mut std::fmt::Formatter }
我是一名优秀的程序员,十分优秀!