What trait can I restrict T
to to allow this to compile?
fn one<T>() -> T {
1.0 as _
}
fn main() {
println!("{}", one::<i8>());
}
As-is, it gives this error:
rustc 1.14.0 (e8a012324 2016-12-16)
error: non-scalar cast: `f64` as `T`
--> <anon>:2:5
|
2 | 1.0 as _
| ^^^^^^^^
A good solution would be a trait that restricts T
to primitive numeric types (i8
, f64
, etc.). I found std::num::Primitive
but it's experimental apparently and the nightly compiler couldn't find it anyway.