You can't use literals like 1.0 for generic types. Float requires One though, so you can write this array as [T::one(); 3]. For arbitrary numbers, you'll have to create that number of type T first, then you can copy it into an array the same way.
Well, you could use NumCast if you want to make 10, and call it like T::from(10.0); Note that NumCast is required to implement Float, so all implementations of Float also implement NumCast
use num::One; // 0.2.0
use num_traits::Float;
struct Foo<T: Float> {
data: [T;3]
}
impl <T: Float> Foo<T> {
fn new() -> Foo<T> {
let data = [One::one();3];
Foo {data}
}
}
fn main() {
let foo = Foo::<Float>::new();
println!("{:?}", foo.data);
}
Hi, folks. I did some modification following advices above, but something wrong.
I do not know how to fix it. Would you please share your soluction about this problem?
Error message:
Compiling playground v0.0.1 (/playground)
error[E0038]: the trait `num_traits::float::Float` cannot be made into an object
--> src/main.rs:16:21
|
16 | let foo = Foo::<Float>::new();
| ^^^^^ the trait `num_traits::float::Float` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
error: aborting due to previous error
For more information about this error, try `rustc --explain E0038`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.