Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何实现在整数上参数化的Rust构造函数?_Rust - Fatal编程技术网

如何实现在整数上参数化的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() }
    }
}