Skip to content

Instantly share code, notes, and snippets.

@cceckman
Created February 20, 2014 07:45
Show Gist options
  • Select an option

  • Save cceckman/9108720 to your computer and use it in GitHub Desktop.

Select an option

Save cceckman/9108720 to your computer and use it in GitHub Desktop.
Trait inheritance in Rust?
gist.rs:12:5: 16:6 error: method `add` is not a member of trait `vector<A>`
gist.rs:12 fn add(&self, rhs : &(A,A,A)) -> (A,A,A){
gist.rs:13 let (x,y,z) = *self;
gist.rs:14 let (a,b,c) = *rhs;
gist.rs:15 (x+a, y+b, c+z)
gist.rs:16 }
gist.rs:11:1: 17:2 error: failed to find an implementation of trait std::ops::Add<(A,A,A),(A,A,A)> for (A,A,A)
gist.rs:11 impl<A : Num> vector<A> for (A, A, A){
gist.rs:12 fn add(&self, rhs : &(A,A,A)) -> (A,A,A){
gist.rs:13 let (x,y,z) = *self;
gist.rs:14 let (a,b,c) = *rhs;
gist.rs:15 (x+a, y+b, c+z)
gist.rs:16 }
use std::num;
use std::ops;
use std::fmt;
trait vector<A : Num> :
Add<Self, Self>
{ }
// points in 3-space
impl<A : Num> vector<A> for (A, A, A){
fn add(&self, rhs : &(A,A,A)) -> (A,A,A){
let (x,y,z) = *self;
let (a,b,c) = *rhs;
(x+a, y+b, c+z)
}
}
pub fn main() -> int{
let x = (3.0, 3.0, 3.0);
let y = (1.0, 1.5, 1.0);
let z = x + y;
let s = format!("Hi, rust! z is ({}, {}, {})", z.n0(), z.n1(), z.n2());
std::io::stdio::println(s);
0;
}
@cceckman

Copy link
Copy Markdown
Author

Compiling gist.rs with rustc gives output in error.log. I fail to see how vector<A> could require implementing std::ops::Add and yet be told that it doesn't meet the signature. I'm guessing I've got the add signature wrong- but how?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment