rustHow to define closure return type in RUst
In Rust, closure return types can be defined by using the ->
operator followed by the return type. For example, the following closure returns a String
:
let closure = |x| -> String { format!("The value of x is {}", x) };
The return type of the closure can also be inferred by the compiler, so the above example can be written as:
let closure = |x| { format!("The value of x is {}", x) };
The return type of the closure can also be explicitly set to ()
if the closure does not return a value. For example:
let closure = |x| -> () { println!("The value of x is {}", x) };
Helpful links
Related
- Using closure variables in Rust
- Is it possible to use closure recursion in Rust
- Example of closure that returns future in Rust
- Nested closure example in Rust
- Are there named closure in Rust
- Using closure inside closure in Rust
- Closure example in Rust
- How to declare a closure in Rust
- How to drop a closure in Rust
More of Rust
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- Hashshet example in Rust
- How to use a tuple as a key in a Rust HashMap?
- How to convert a Rust HashMap to JSON?
- How to replace strings using Rust regex?
- How to use regex to match a double quote in Rust?
- How to get a capture group using Rust regex?
See more codes...