One way I know is to provide the type annotations in Rust is by declaring an intermediate variable so the compiler knows the return type:
use std::num::Int
let max_usize: usize = Int::max_value();
println!("Max usize: {}", max_usize);
But how can I provide the type annotation "inline"?
For example, I don't expect the following to work unmodified because there's no type annotation at all, but this is the kind of thing I'm after:
use std::num::Int
println!("Max usize: {}", Int::max_value());
I tried Int::max_value::<usize>()
, which gives error: too many type parameters provided: expected at most 0 parameter(s), found 1 parameter(s)
- and that makes sense because max_value()
isn't generic.
In Scala I would write myFunction(someDog: Animal)
instead of writing
val someAnimal: Animal = someDog
myFunction(someAnimal)
Is there equivalent syntax in Rust?
Like so:
fn main() {
use std::num::Int;
println!("Max usize: {}", <usize as Int>::max_value());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With