January 2019
Beginner to intermediate
554 pages
13h 31m
English
Closures that access variables only for read access implement the Fn trait. Any value they access are as reference types (&T). This is the default mode of borrowing the closures assumes. Consider the following code:
// fn_closure.rsfn main() { let a = String::from("Hey!"); let fn_closure = || { println!("Closure says: {}", a); }; fn_closure(); println!("Main says: {}", a);}
We get the following output upon compilation:
Closure says: Hey!Main says: Hey!
The a variable was still accessible even after invoking the closure as the closure used a by reference.
Read now
Unlock full access