I am trying to understand a some concepts in Rust but I am stuck in a very simple problem. I am trying to define a struct which I then want to print. If I specify the type of the components (in the example replace T
by f32
), everything is fine. But if I want to do it generically:
#[deriving(Show)]
struct Point<T> {
x: T,
y: T,
z: T,
}
fn main() {
let v = Point{x: 3., y: 4., z: 5.,};
println!("The point is {}" , v);
}
The output in http://play.rust-lang.org/ is:
error: unable to infer enough type information to locate the impl of the trait
core::fmt::Show
for the type_
; type annotations required
If I try to specify the type:
use std::fmt;
#[deriving(Show)]
struct Point<T: std::fmt::Show> {
x: T,
y: T,
z: T,
}
fn main() {
let v = Point{x: 3., y: 4., z: 5.,};
println!("The point is {}" , v);
}
The output is:
error: trait
std::fmt::Show
already appears in the list of bounds [E0127] previous appearance is here #[deriving(Show)]
Why is this? How to solve it?