:nerd_face: let myfn = |a: i32 | -> i32 {a + 1};
:nerd_face: let myfn = |a| -> a + 1;
:nerd_face: To be anonymous move away () to ||
:confounded: Understand the difference between annotated vs. inferred
:hammer: Do you understand this: let myfn = || 1;
:nerd_face: Remember to end with a semicolon for all closures i.e. anonymous functions
:bulb: What are the optional(s) then?
Type
{}
->
:bulb: What is mandatory?
;
fn main() {
// Increment via closures and functions.
fn function (i: i32) -> i32 { i + 1 }
// Closures are anonymous, here we are binding them to references
// Annotation is identical to function annotation but is optional
// as are the `{}` wrapping the body. These nameless functions
// are assigned to appropriately named variables.
let closure_annotated = |i: i32| -> i32 { i + 1 };
let closure_inferred = |i | i + 1 ;
let i = 1;
// Call the function and closures.
println!("function: {}", function(i));
println!("closure_annotated: {}", closure_annotated(i));
println!("closure_inferred: {}", closure_inferred(i));
// A closure taking no arguments which returns an `i32`.
// The return type is inferred.
let one = || 1;
println!("closure returning one: {}", one());
}
:nerd_face:
let myfn = |a: i32 | -> i32 {a + 1};
:nerd_face:let myfn = |a| -> a + 1;
:nerd_face: To be anonymous move away()
to||
:confounded: Understand the difference between annotated vs. inferred :hammer: Do you understand this:let myfn = || 1;
:nerd_face: Remember to end with a semicolon for all closures i.e. anonymous functions:bulb: What are the optional(s) then?
Type
{}
->
:bulb: What is mandatory?
;