这是场景:我有一个结构和特征对,如下所示:
trait Operation {
fn operation(self) -> u32
}
struct Container<T: Sized + Operation> {
x:T
}
impl <T: Sized + Operation> for Container<T> {
fn do_thing(&self) -> u32 {
// Do something with the field x.
}
}
Run Code Online (Sandbox Code Playgroud)
无论何时使用,该操作都需要按值传递,并且问题与"do_thing"类似.我宁愿不必为该类型强制执行复制语义,T并希望为此解决此问题.基本上我想知道以下内容:
struct Container<T: Sized + Operation> where &T: Operation { ... }.我尝试了一下语法,但我没有取得任何成功.Middle: Operation,在那里Middle可以要求任何实施者Middle,T,也需要实现Operation的&T.一些说明:
Operation特性,它是给定的,这就是我要使用的东西.