我做了一个两元素Vector结构,我想重载+运算符.
我使我的所有函数和方法都采用引用而不是值,我希望+运算符以相同的方式工作.
impl Add for Vector {
fn add(&self, other: &Vector) -> Vector {
Vector {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据我尝试的变化,我要么遇到生命问题,要么输入不匹配.具体来说,这个&self论点似乎没有被视为正确的类型.
我已经看到了模板参数的例子上impl,以及Add,但他们只是导致不同的错误.
我发现如何为不同的RHS类型和返回值重载运算符?但即使我把一个use std::ops::Mul;放在顶部,答案中的代码也不起作用.
我正在使用rustc 1.0.0-nightly(ed530d7a3 2015-01-16 22:41:16 +0000)
我不接受"你只有两个字段,为什么要使用参考"作为答案; 如果我想要一个100元素结构怎么办?我会接受一个答案,证明即使有一个大的结构我也应该通过值传递,如果是这样的话(我认为不是这样).我有兴趣知道结构大小的一个好的经验法则并且通过值vs struct传递,但这不是当前的问题.
我最初假设您可以这样做,因为文档(http://doc.rust-lang.org/rust.html#implementations)建议您可以:
trait Bar<T> {
fn ex(&self) -> T;
}
struct Foo {
y:f64
}
impl Bar<int> for Foo {
fn ex(&self) -> int {
return self.y.floor() as int;
}
}
impl Bar<uint> for Foo {
fn ex(&self) -> uint {
if (self.y < 0.0) {
return 0u;
}
return self.y.floor() as uint;
}
}
Run Code Online (Sandbox Code Playgroud)
......但这似乎不起作用.我得到的错误如下:
error: multiple applicable methods in scope
error: expected Bar<uint>, but found Bar<int> (expected uint but found int)
error: expected Bar<int>, but found Bar<uint> …Run Code Online (Sandbox Code Playgroud) 我有一个自定义类型pub struct Foo,我希望能够说字符串可以转换为Foo类型.我正在努力impl<'a> Into<Foo> for &'a str,但我从这个答案中知道我不能这样做.我还有其他选择吗?
对于上下文,我正在尝试做类似的事情
trait Foo {
type ArgType;
fn new<I: Into<Self::ArgType>>(arg: I) -> Self;
}
struct MyType;
impl Into<MyType> for str {
fn into(self) -> MyType { MyType }
}
struct Bar;
impl Foo for Bar {
type ArgType = MyType;
fn new<I: Into<MyType>>(arg: I) -> Bar { Bar }
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Rust中编写一些通用的数学函数,并且我一直遇到以下错误消息:
error: conflicting implementations for trait SoAndSo
Run Code Online (Sandbox Code Playgroud)
有可能解决问题吗?如果是这样,怎么样?
例如,我正在尝试编写一个带有两个迭代器的通用点积,拉链它们并迭代它们以积累产品.我希望这个功能也能够计算复值点产品.复数上的点积包括共轭一侧.我的第一个想法是Dot1为二元函数写一个特征来替换Mul,因为它也会使左侧参数共轭.这是完整的代码:
extern crate num;
use num::complex::Complex;
use num::{Float, Num};
trait Dot1<Rhs, Result> {
fn dot1(&self, rhs: Rhs) -> Result;
}
impl<T: Float> Dot1<T, T> for T {
// conjugation for reals is a no-op
fn dot1(&self, rhs: T) -> T {
*self * rhs
}
}
impl<T: Num + Clone> Dot1<Complex<T>, Complex<T>> for Complex<T> {
fn dot1(&self, rhs: Complex<T>) -> Complex<T> {
self.conj() * rhs
}
}
fn …Run Code Online (Sandbox Code Playgroud) 如如何为不同的 RHS 类型和返回值重载运算符?您可以实现一些运算符,例如。使用变通方法添加多种类型。
PartialEq 特质是否有类似的可能?
我已经尝试了各种方法,但我能得到的最接近的是创建一个假特征 Foo,在 &Foo 上实现 PartialEq(因为它是一个你不能在 Foo 上实现它的特征),然后做:
let x:Bar = ...
let y:FooBar = ...
if &x as &Foo == &y as &Foo {
...
}
Run Code Online (Sandbox Code Playgroud)
Equiv trait 看起来应该用于此目的,但据我所知,实现 Equiv 与 == 运算符没有任何关系。
有没有办法做到这一点?
我正在尝试做一些非常简单的事情:
fn main() {
#[deriving(Show)]
struct A {
a: int
}
impl Add<A, A> for A {
fn add(&self, other: &A) -> A {
A { a: self.a + other.a }
}
}
impl Add<int, A> for A {
fn add(&self, v: &int) -> A {
A { a: self.a + *v }
}
}
let x = A { a: 10 } + A { a: 20 };
println!("x: {}", x);
}
Run Code Online (Sandbox Code Playgroud)
Rust compile 不喜欢我的代码并说:
src/sandbox.rs:20:12: 20:37 error: multiple …Run Code Online (Sandbox Code Playgroud)