如何实现在整数上参数化的Rust构造函数?
我想构造一个在整数上参数化的对象。尝试了以下操作:如何实现在整数上参数化的Rust构造函数?,rust,Rust,我想构造一个在整数上参数化的对象。尝试了以下操作: struct Alpha<T> { num: T, } impl<T: Integer> Alpha<T> { fn new() -> Alpha<T> { Alpha { num: 0 } } } 代码是。怎么了 怎么了 这是: struct Foo; impl Integer for Foo { … } Alpha::<Foo>::
struct Alpha<T> {
num: T,
}
impl<T: Integer> Alpha<T> {
fn new() -> Alpha<T> {
Alpha { num: 0 }
}
}
代码是。怎么了
怎么了
这是:
struct Foo;
impl Integer for Foo { … }
Alpha::<Foo>::new() // This should work as `Foo: Integer` and that's
// the only condition on `Alpha::new`.
// But it would need to create a instance
// of `Foo` from `0`.
// But the compiler has no idea how to do that!
谢谢我不遵循Foo的例子,但我认为它不像我在Haskell中习惯的那样简单。我试图更好地解释Foo的例子。
struct Foo;
impl Integer for Foo { … }
Alpha::<Foo>::new() // This should work as `Foo: Integer` and that's
// the only condition on `Alpha::new`.
// But it would need to create a instance
// of `Foo` from `0`.
// But the compiler has no idea how to do that!
impl<T: Integer> Alpha<T> {
fn new() -> Alpha<T> {
Alpha { num: Zero::zero() }
}
}