【发布时间】:2016-08-03 12:52:48
【问题描述】:
我打算将以下结构与 i32、usize 等简单的数字类型一起使用,但在 contains() 方法的实现中,Rust 迫使我使用 Clone 而不是隐式副本(“不能搬出借来的内容”)。
我没有找到任何可以告诉编译器T 是带有隐式复制的简单类型的特征。我可以指望编译器会为数字类型丢弃 clone() 调用,而不会影响性能吗?
pub struct Rect<T> {
pub x: T,
pub y: T,
pub w: T,
pub h: T,
}
impl<T> Rect<T> where T: PartialOrd + Add<T, Output=T> + Clone {
pub fn contains(&self, x: T, y: T) -> bool {
x >= self.x && y >= self.y
&& x < self.x.clone() + self.w.clone()
&& y < self.y.clone() + self.h.clone()
}
}
【问题讨论】: