如何将一个字段的生命周期指定为其他字段的组合?

How do I specify the lifetime of a field as a combination of other fields?

我有一个函数将两个参数存储在一个结构中的三个引用字段中。我不知道如何指定第三个结果字段的生命周期,它是函数前两个参数生命周期的组合。

我尝试将前两个引用参数存储在结构中。这工作得很好,没有兴趣。更有趣的是我在下面展示的案例,我没有解决方案。

我知道这段代码没有任何意义;它只是说明了问题。

// This function can be found in "Lifetime Annotations in Function Signatures" of the Rust manual
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

// Here comes the interesting part; 1st the result type of my function
struct SillyResult<'a, 'b, 'c> {
    arg1: &'a str,
    arg2: &'b str,
    result: &'c str,
}

// ... and now the function, that does not compile and shall be corrected
fn silly_fkt<'a, 'b, 'c: 'a + 'b>(arg1: &'a str, arg2: &'b str) -> SillyResult<'a, 'b, 'c> {
    // Neither the following line ...
    // SillyResult<'a, 'b, 'c>{arg1: arg1, arg2: arg2, result: longest(arg1, arg2)}
    // ... nor the following line work
    SillyResult {
        arg1,
        arg2,
        result: longest(arg1, arg2),
    }
}

我们的想法是将生命周期 'a'b 合并为生命周期 'c。 然而,它给出了一堆抱怨生命周期的错误:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in function call due to conflicting requirements
  --> src/lib.rs:25:17
   |
25 |         result: longest(arg1, arg2),
   |                 ^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'b as defined on the function body at 18:18...
  --> src/lib.rs:18:18
   |
18 | fn silly_fkt<'a, 'b, 'c: 'a + 'b>(arg1: &'a str, arg2: &'b str) -> SillyResult<'a, 'b, 'c> {
   |                  ^^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:25:31
   |
25 |         result: longest(arg1, arg2),
   |                               ^^^^
note: but, the lifetime must be valid for the lifetime 'c as defined on the function body at 18:22...
  --> src/lib.rs:18:22
   |
18 | fn silly_fkt<'a, 'b, 'c: 'a + 'b>(arg1: &'a str, arg2: &'b str) -> SillyResult<'a, 'b, 'c> {
   |                      ^^
   = note: ...so that the expression is assignable:
           expected SillyResult<'a, 'b, 'c>
              found SillyResult<'_, '_, '_>

我试图将 silly_fkt 的最后一行更改为

SillyResult<'a, 'b, 'c>{ arg1, arg2, result: longest(arg1, arg2) }

但这不起作用。

silly_fkt 的正确代码是什么?

你有 : 倒过来的语义:'c: 'a 意味着 'c outlives 'a,你想说 'c 'a 长寿(这样你就可以提供生命周期 'a 的参考,其中生命周期 'c 是预期的)。所以你需要反过来写生命周期约束。

您可以写 <'a: 'c, 'b: 'c, 'c>,但我发现使用 where 子句更容易阅读:

fn silly_fkt<'a, 'b, 'c>(arg1: &'a str, arg2: &'b str) -> SillyResult<'a, 'b, 'c>
where
    'a: 'c,
    'b: 'c,
{
    SillyResult {
        arg1,
        arg2,
        result: longest(arg1, arg2),
    }
}