选择借用或传递所有权,而不复制Rust中的代码
我希望我可以选择传递ref或对象本身,而不必多次编写相同的函数,如下所示:选择借用或传递所有权,而不复制Rust中的代码,rust,Rust,我希望我可以选择传递ref或对象本身,而不必多次编写相同的函数,如下所示: use std::ops::Add; #[derive(Debug)] struct Point { x: i32, y: i32, } pub trait Add1<RHS=Self> { type Output; fn add1(&self, rhs: &RHS) -> Self::Output; } impl Add for Point {
use std::ops::Add;
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
pub trait Add1<RHS=Self> {
type Output;
fn add1(&self, rhs: &RHS) -> Self::Output;
}
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}
impl Add1 for Point {
type Output = Point;
fn add1(&self, other: &Point) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}
fn main() {
let p1 = Point { x: 1, y: 0 };
let p2 = Point { x: 2, y: 3 };
let p4 = p1.add1(&p2);
let p3 = p1 + p2;
println!("{:?}", p3);
println!("{:?}", p4);
}
使用std::ops::Add;
#[导出(调试)]
结构点{
x:i32,
y:i32,
}
发布特性地址1{
类型输出;
fn add1(&self,rhs:&rhs)->self::Output;
}
点的impl Add{
类型输出=点;
fn添加(自身,其他:点)->点{
点{x:self.x+other.x,y:self.y+other.y}
}
}
点的impl Add1{
类型输出=点;
fn add1(自身,其他:&点)->点{
点{x:self.x+other.x,y:self.y+other.y}
}
}
fn main(){
设p1=点{x:1,y:0};
设p2=点{x:2,y:3};
设p4=p1.add1(&p2);
设p3=p1+p2;
println!(“{:?}”,p3);
println!(“{:?}”,p4);
}
如何避免这种冗余?您可以使用来抽象借用的类型
use std::ops::Add;
use std::borrow::Borrow;
#[derive(Clone, Debug)]
struct Point {
x: i32,
y: i32,
}
impl<T: Borrow<Point>> Add<T> for Point {
type Output = Point;
fn add(self, other: T) -> Point {
Point { x: self.x + other.borrow().x, y: self.y + other.borrow().y }
}
}
fn main() {
let p1 = Point { x: 1, y: 0 };
let p2 = Point { x: 2, y: 3 };
let p4 = p1.clone() + &p2;
let p3 = p1 + p2;
println!("{:?}", p3);
println!("{:?}", p4);
}
使用std::ops::Add;
使用std::借用::借用;
#[派生(克隆、调试)]
结构点{
x:i32,
y:i32,
}
点的impl Add{
类型输出=点;
fn添加(自身,其他:T)->点{
点{x:self.x+other.borrow().x,y:self.y+other.borrow().y}
}
}
fn main(){
设p1=点{x:1,y:0};
设p2=点{x:2,y:3};
设p4=p1.clone()+&p2;
设p3=p1+p2;
println!(“{:?}”,p3);
println!(“{:?}”,p4);
}
如果为点实现添加,该怎么办
use std::ops::Add;
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
println!("refadd");
Point { x: self.x + other.x, y: self.y + other.y }
}
}
impl<'a> Add for &'a Point {
type Output = Point;
fn add(self, other: &Point) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}
fn main() {
let p1 = Point { x: 1, y: 0 };
let p2 = Point { x: 2, y: 3 };
let p4 = &p1 + &p2;
let p3 = p1 + p2;
println!("{:?}", p3);
println!("{:?}", p4);
}
使用std::ops::Add;
#[导出(调试)]
结构点{
x:i32,
y:i32,
}
点的impl Add{
类型输出=点;
fn添加(自身,其他:点)->点{
println!(“refadd”);
点{x:self.x+other.x,y:self.y+other.y}
}
}
内点{
点{x:self.x+other.x,y:self.y+other.y}
}
}
fn main(){
设p1=点{x:1,y:0};
设p2=点{x:2,y:3};
设p4=&p1+&p2;
设p3=p1+p2;
println!(“{:?}”,p3);
println!(“{:?}”,p4);
}
您的示例似乎有多个合并的关注点。这涉及到两种不同的特征,通常特征具有不同的含义,因此您不希望完全重用它们。你到底想做什么?为什么借用而不是AsRef
?AsRef
并不是对所有T
都实现,借用是。在这里Cow
会有帮助吗?@qed使用Cow
需要创建调用站点中明确的Cow
实例,Cow
是一个枚举,这不是一个特性。如果能够为T执行类似于impl Add的操作会很好,其中T:Borrow
,但这会导致E0210。我如何解决这个问题?这似乎没有解决如何避免这种冗余?。也许你可以更新它这样做?