Generics 泛型数学运算符重载而不复制
阅读之后,我会有一个关于重载数学运算符的类似问题 考虑到这个代码基础,它假设Generics 泛型数学运算符重载而不复制,generics,math,rust,Generics,Math,Rust,阅读之后,我会有一个关于重载数学运算符的类似问题 考虑到这个代码基础,它假设T对象可以通过值进行添加,并给出一个O: use std::ops::Add; struct NoCopy<T>(T); impl<T: Add<Output = O>, O> Add for NoCopy<T> { type Output = NoCopy<O>; fn add(self, other: Self) -> Self:
T
对象可以通过值进行添加,并给出一个O
:
use std::ops::Add;
struct NoCopy<T>(T);
impl<T: Add<Output = O>, O> Add for NoCopy<T> {
type Output = NoCopy<O>;
fn add(self, other: Self) -> Self::Output {
NoCopy(self.0 + other.0)
}
}
fn main() {
let a = NoCopy::<isize>(5);
let b = NoCopy::<isize>(3);
let _c = a + b;
}
缺少的部分(
?
)是什么样子的?您可以对&T
设置寿命限制:
impl<'a, T: 'a, O> Add for &'a NoCopy<T>
where
&'a T: Add<Output = O>,
{
type Output = NoCopy<O>;
fn add(self, other: Self) -> Self::Output {
NoCopy(&self.0 + &other.0)
}
}
impl Add for&'a NoCopy
哪里
&"答:补充,,
{
类型输出=NoCopy;
fn add(self,other:self)->self::Output{
NoCopy(&self.0+&other.0)
}
}
()
impl<'a, T: 'a, O> Add for &'a NoCopy<T>
where
&'a T: Add<Output = O>,
{
type Output = NoCopy<O>;
fn add(self, other: Self) -> Self::Output {
NoCopy(&self.0 + &other.0)
}
}