rust - 如何编写支持+=操作的特征绑定,其右手是Rust中复杂情况下的引用
问题描述
我想写一个支持添加操作的 Vector 结构,并写一个使用 Vector 结构的一些特征,所以我写了这个。
use std::ops::*;
#[derive(Clone)]
struct Vector<T>(Vec<T>);
impl<'a, T> Add<&'a Vector<T>> for Vector<T>
where
T: AddAssign<&'a T>,
{
type Output = Vector<T>;
fn add(mut self, rhs: &'a Vector<T>) -> Self::Output {
self.0
.iter_mut()
.zip(rhs.0.iter())
.for_each(|(left, right)| {
*left += right;
});
self
}
}
trait SomeOperation<'a ,T>
where
T: AddAssign<&'a T>+Clone + 'a,
{
fn add(u:Vector<T>,v:&'a Vector<T>)->Vector<T>{
let w = u+v;
let x = v.clone()+&w;
x
}
}
但是会出现编译错误。
21 | trait SomeOperation<'a ,T>
| -- lifetime `'a` defined here
...
27 | let x = v.clone()+&w;
| ^^
| |
| borrowed value does not live long enough
| requires that `w` is borrowed for `'a`
28 | x
29 | }
| - `w` dropped here while still borrowed
我怎样才能避免这些类型的错误。
解决方案
您谈论实现AddAssign
,但您的代码试图实现Add
. 此外,我无法弄清楚SomeOperation
是为了什么。我将Debug
特征添加到派生线。
use std::ops::*;
#[derive(Clone, Debug)]
struct Vector<T>(Vec<T>);
impl<'a, T> AddAssign<&'a Vector<T>> for Vector<T>
where
T: AddAssign<&'a T>
{
fn add_assign(&mut self, rhs: &'a Vector<T>) {
self.0
.iter_mut()
.zip(rhs.0.iter())
.for_each(|(left, right)| {
*left += right;
});
}
}
impl<'a, 'b, T> Add<& 'b Vector<T>> for & 'a Vector<T>
where
Vector<T>: AddAssign<& 'b Vector<T>>,
T: Clone,
{
type Output = Vector<T>;
fn add(self, other: & 'b Vector<T>) -> Self::Output {
let mut res: Vector<T> = self.clone();
res += other;
res
}
}
fn main() {
let mut v1: Vector<u32> = Vector(vec![1, 2, 3]);
let v2 = Vector(vec![4, 5, 6]);
println!("Add: {:?}", &v1 + &v2);
v1 += &v2;
println!("AddAssign{:?}", v1);
}
推荐阅读
- excel - 尝试粘贴到不同工作簿的底部
- ios - Swift Notification Center 导致内存泄漏
- java - 仅使用 JRE 运行的 Eclipse
- c++ - for-loop:指针变量作为 if 语句中的条件的必要性
- python - 直接比较两张图像,当一张有 alpha 通道时?
- screwdriver-cd - 螺丝刀.cd 中的构建作业消耗的资源
- javascript - setState 的同步替代方案
- django - Django 视图不保存模型实例但正确运行数据处理功能?
- css - Ionic v3 在运行时在明暗主题之间切换(动态)
- auth0 - 使用 Auth0 的 Lock 进行身份验证后检索 id 令牌