The return type of a block is inferred.
fn main() {
let x = { 5 };
println!("{}", x);
}
But when I give the block a name, I have to specify a type.
fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("{}", x);
}
How can I avoid selecting a type?
I can obtain the type of the function by using typeof : type t = typeof test; Here, t will be () => number .
The result of a function is called its return value and the data type of the return value is called the return type. Every function declaration and definition must specify a return type, whether or not it actually returns a value.
Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.
You cannot. Rust explicitly prohibits this by design.
However, for large and complex return types, you have the following options:
You can see a practical example of these in the answer for What is the correct way to return an Iterator (or any other trait)?
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