I'm playing with some Rust Traits and Generics to get familiar with the language.
fn main() {
println(test(6f).to_str());
}
enum Result<TS,TE>{
Success(TS),
Error(TE)
}
impl<TS: ToStr, TE: ToStr> ToStr for Result<TS,TE> {
fn to_str(&self) -> ~str {
match *self {
Success(s) => s.to_str(),
Error(e) => e.to_str()
}
}
}
fn test(x:float) -> Result<float,int> {
match x {
0f..5f => Success(x/5f),
_ => Error(1i)
}
}
I get the folloing errow with the code above.
C:\Users\mflamer\Dropbox\Rust Projects\Tests\rust.rs:27:8: 27:13 error: moving out of dereference of immutable & pointer C:\Users\mflamer\Dropbox\Rust Projects\Tests\rust.rs:27 match *self
^~~~~
It builds fine without the generics on the trait. Whats going on here?
Edit: If I change the code to this it works. Not sure why.
enum Result<TS,TE>{
Success{ value:TS },
Error{ error:TE }
}
impl<TS: ToStr, TE: ToStr> ToStr for Result<TS,TE> {
fn to_str(&self) -> ~str {
match *self {
Success{ value: value } => value.to_str(),
Error{ error: error } => error.to_str()
}
}
}
fn test(x:float) -> Result<float,int> {
match x {
0f..5f => Success{ value: x/5f },
_ => Error{ error: 1i }
}
}