How can I provide inline type annotations when calling a non-generic function?

One way I know is to provide type annotations in Rust by declaring an intermediate variable so that 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 an inline annotation?

For example, I do not expect the following to work unchanged, because there is no type annotation at all, but this is what I do 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() not common.

In Scala, I would write myFunction(someDog: Animal) instead of writing

 val someAnimal: Animal = someDog myFunction(someAnimal) 

Is there an equivalent syntax in Rust?

+6
source share
1 answer

Same:

 fn main() { use std::num::Int; println!("Max usize: {}", <usize as Int>::max_value()); } 
+4
source

Source: https://habr.com/ru/post/981859/


All Articles