我试图在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
。 / p>
尝试#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
应该与整个函数体一样长或更长,而我只是想要它意味着&#34;任何寿命&#34;
我在Rust中尝试做什么?如果没有,是否有任何建议的更改我应该注意哪些可以实现?
答案 0 :(得分:4)
扣上......
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
}
够简单吧? ; - )
让我们一步一步......
您希望接受对类型的引用,但引用需要实现Add
。 where
条款允许您在:
的任意一侧编写复杂类型,因此我们使用&'a T: Add
。
这将返回一些我们接受另一个引用的值。但是,double_and_square
的调用者无法指定生命周期,因为它只存在于函数内部。这意味着我们需要使用更高排名的特质绑定:for <'b>
。
我们必须使用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
的具体类型传递,因此应该可以在任何类型中使用放置原件,并可能在更多情况下使用。
我希望它适用于
的类型T
为非Copy
对类型的不可变引用始终为Copy
,即使类型本身并未实现Copy
。因此,您可以使用例如T = i32
或一个T = &NonCopy
。 仅接受引用的原始案例只接受第二个。
在一个理想的世界中,您可以避免使用通用类型R
,只需说<...something...>::Output
,但据我所知,目前还不可能。< / p>