rust - 你如何结合使用rust 的生命周期?

标签 rust

这段代码:

struct Foo<'a> {
  value: Option<&'a int>,
  parent: Option<&'a Foo<'a>>
}

impl<'a> Foo<'a> {
  fn bar<'a, 'b, 'c: 'a + 'b>(&'a self, other:&'b int) -> Foo<'c> {
    return Foo { value: Some(other), parent: Some(self) };
  }
}

fn main() {
  let e = 100i;
  {
    let f = Foo { value: None, parent: None };
    let g:Foo;
    {
       g = f.bar(&e);
    }
    // <--- g should be valid here
  }
  // 'a of f is now expired, so g should not be valid here.

  let f2 = Foo { value: None, parent: None };
  {
    let e2 = 100i;
    let g:Foo;
    {
       g = f2.bar(&e2);
    }
    // <--- g should be valid here
  }
  // 'b of e2 is now expired, so g should not be valid here.
}

编译失败,错误:

<anon>:8:30: 8:35 error: cannot infer an appropriate lifetime due to conflicting requirements
<anon>:8     return Foo { value: Some(other), parent: Some(self) };
                                      ^~~~~
<anon>:7:3: 9:4 note: consider using an explicit lifetime parameter as shown: fn bar<'a, 'b>(&'a self, other: &'b int) -> Foo<'b>
<anon>:7   fn bar<'a, 'b, 'c: 'a + 'b>(&'a self, other:&'b int) -> Foo<'c> {
<anon>:8     return Foo { value: Some(other), parent: Some(self) };
<anon>:9   }

(围栏:http://is.gd/vAvNFi)

这显然是一个人为的例子,但这是我偶尔想做的事情。

所以...

1) 如何组合生命周期? (即返回一个生命周期至少为 'a 或 'b 的 Foo,以较短者为准)

2) 有什么方法可以编写 Assets 生命周期编译失败的测试吗? (例如,尝试编译一个以错误方式使用该函数并因生命周期错误而失败的#[test])

最佳答案

边界 'c: 'a + 'b意味着 'c至少与 'a 一样长只要'b .但是,在这种情况下,Foo值恰好对 'a 中最短的一个有效和 'b : 一旦任何一个引用背后的数据超出范围,整个 Foo必须作废。 (这是说对 'c 有效的数据在 'a'b并集中也是有效的。)

更具体地说,比如说 'b = 'static , 然后 'c: 'a + 'static意味着 'c也必须是 'static ,所以返回值为 Foo<'static> .这显然是不对的,因为它会“升级”有限的 'a self成为永恒的引用。

此处正确的行为是采用生命周期的交集:Foo仅在两个函数参数都有效时才有效。交集操作只是标记具有相同名称的引用:

fn bar<'a>(&'a self, other: &'a int) -> Foo<'a>

关于rust - 你如何结合使用rust 的生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677402/

相关文章:

rust - 是否可以在模式中创建可变引用的可变值?

floating-point - 如何处理 Rust 中不精确的浮点运算结果?

rust - 尝试在 Rust 程序中使用 &self

types - 什么时候将数字文字分配给默认类型?

rust - 从格式化的输入构建树

sorting - rust 添加.sort_by生成错误: expected struct `Vec` ,发现 `()`

ssl - Cargo 的证书颁发机构商店在哪里?

rust - 如何基于快速错误扩展错误?

rust - 使用rust lazy_static!具有后处理字符串的宏?

proxy - 企业代理背后的 Windows 上的 cargo