0
我想了解Rust中的一些概念,但我陷入了一個非常簡單的問題。我試圖定義一個結構,然後我想打印。如果我指定組件的類型(在示例中用代替T
),則一切正常。但是,如果我想這樣做一般:派生顯示在一個通用的結構
#[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);
}
在http://play.rust-lang.org/的輸出是:
error: unable to infer enough type information to locate the impl of the trait
core::fmt::Show
for the type_
; type annotations required
如果我嘗試指定類型:
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);
}
輸出是:
error: trait
std::fmt::Show
already appears in the list of bounds [E0127] previous appearance is here #[deriving(Show)]
這是爲什麼?如何解決它?
我想我underspecifying的結構,不,我是實例化結構錯了。 – Hernan 2014-12-27 17:02:47
@Hernan:第一個錯誤的確是由於低估了結構的成員造成的。第二個錯誤似乎是由'#[deriving(Show)]'自動添加'T'上的'Show'綁定引起的;通過自己添加它,它最終會出現兩次。 – 2014-12-28 01:13:42