1
votes

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?

1

1 Answers

1
votes

3. isn't specific enough to name a type on it's own - it could be either f32 of f64. You could be more explicit in (at least) these two ways:

let v = Point{x: 3f32, y: 4f32, z: 5f32};
let v: Point<f32> = Point{x: 3., y: 4., z: 5.};