gpt4 book ai didi

generics - 如何为涉及对中间局部变量的引用的闭包指定生命周期边界?

转载 作者:行者123 更新时间:2023-11-29 07:51:02 25 4
gpt4 key购买 nike

我正在尝试用 Rust 编写如下函数:

fn double_and_square<'a, T>(x: &'a T) -> /* whatever the output type of `&t * &t` is */ {
let t = x + x;
&t * &t
}

我希望它在 T 是非 Copy 的类型上工作。我不仅需要指定 &'a T 实现 Add(简单),还需要指定对其输出类型的引用以及局部变量 t 的生命周期 实现 Mul

尝试 #1(没有为中间类型指定生命周期):

fn double_and_square<'a, T>(x: &'a T) -> <&<&'a T as Add>::Output as Mul>::Output
where
&'a T: Add,
&<&'a T as Add>::Output: Mul,
{
let t = x + x;
&t * &t
}

导致以下编译器错误:

error[E0106]: missing lifetime specifier
--> src/main.rs:6:5
|
6 | &<&'a T as Add>::Output: Mul,
| ^ expected lifetime parameter

尝试 #2(好的,我将添加生命周期说明符):

fn double_and_square<'a, 'b, T>(x: &'a T) -> <&'b <&'a T as Add>::Output as Mul>::Output
where
&'a T: Add,
&'b <&'a T as Add>::Output: Mul,
{
let t = x + x;
&t * &t
}

导致以下编译器错误:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:8:13
|
8 | let t = x + x;
| ^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 3:1...
--> src/main.rs:3:1
|
3 | / fn double_and_square<'a, 'b, T>(x: &'a T) -> <&'b <&'a T as Add>::Output as Mul>::Output
4 | | where
5 | | &'a T: Add,
6 | | &'b <&'a T as Add>::Output: Mul,
... |
9 | | &t * &t
10| | }
| |_^
note: ...so that expression is assignable (expected &T, found &'a T)
--> src/main.rs:8:13
|
8 | let t = x + x;
| ^
note: but, the lifetime must be valid for the lifetime 'b as defined on the function body at 3:1...
--> src/main.rs:3:1
|
3 | / fn double_and_square<'a, 'b, T>(x: &'a T) -> <&'b <&'a T as Add>::Output as Mul>::Output
4 | | where
5 | | &'a T: Add,
6 | | &'b <&'a T as Add>::Output: Mul,
... |
9 | | &t * &t
10| | }
| |_^
note: ...so that the type `<&T as std::ops::Add<&'a T>>::Output` is not borrowed for too long
--> src/main.rs:9:10
|
9 | &t * &t
| ^^

error[E0490]: a value of type `<&T as std::ops::Add<&'a T>>::Output` is borrowed for too long
--> src/main.rs:9:10
|
9 | &t * &t
| ^^
|
note: the type is valid for the lifetime 'b as defined on the function body at 3:1
--> src/main.rs:3:1
|
3 | / fn double_and_square<'a, 'b, T>(x: &'a T) -> <&'b <&'a T as Add>::Output as Mul>::Output
4 | | where
5 | | &'a T: Add,
6 | | &'b <&'a T as Add>::Output: Mul,
... |
9 | | &t * &t
10| | }
| |_^
note: but the borrow lasts for the lifetime 'a as defined on the function body at 3:1
--> src/main.rs:3:1
|
3 | / fn double_and_square<'a, 'b, T>(x: &'a T) -> <&'b <&'a T as Add>::Output as Mul>::Output
4 | | where
5 | | &'a T: Add,
6 | | &'b <&'a T as Add>::Output: Mul,
... |
9 | | &t * &t
10| | }
| |_^

我阅读 the lifetime must be valid for the lifetime 'b as defined on the function body 告诉我编译器认为 'b 应该存在与整个函数体一样长或更长,而我只希望它表示“任何生命周期”。

我想做的事情在 Rust 中是否可行?如果没有,是否有任何我应该注意的建议更改可以使之成为可能?

最佳答案

系好安全带......

use std::ops::{Add, Mul};

fn double_and_square<'a, T, R>(x: &'a T) -> R
where
&'a T: Add,
for<'b> &'b <&'a T as Add>::Output: Mul<Output = R>,
{
let t = x + x;
&t * &t
}

很简单,对吧? ;-)

让我们一步步来......

  1. 您希望接收对类型的引用,但引用 需要实现 Add . where子句让您可以在 : 的任一侧编写复杂类型, 所以我们使用 &'a T: Add .

  2. 这将返回一些我们引用的值。但是,double_and_square来电者无法指定生命周期,因为它只存在于函数内部。这意味着我们需要使用更高级别的特征界限:for <'b> .

  3. 我们必须使用 Add 的输出类型操作,说它实现了Mul ,输出类型是通用的 R .


我建议不要在原始函数中引用,因为这样更容易理解:

fn double_and_square<T, R>(x: T) -> R
where
T: Add + Copy,
for<'a> &'a T::Output: Mul<Output = R>,
{
let t = x + x;
&t * &t
}

&Foo Foo不同的类型并且可以作为 T 的具体类型传递,所以这应该可以用在原件所在的任何地方,并且可能在更多情况下可用。

I want it to work on types where T is non-Copy

类型的不可变引用总是 Copy ,即使类型本身没有实现 Copy .因此,您可以使用例如调用此函数T = i32 一个T = &NonCopy . 接受引用的原始情况将只接受第二个。

在理想情况下,您可以避免使用泛型类型 R就说<...something...>::Output ,但据我所知目前还不可能。

关于generics - 如何为涉及对中间局部变量的引用的闭包指定生命周期边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49341520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com