Hi all,
I'm new to Rust and while working my way through rustbyexample.com, I've come across something I don't understand.
http://rustbyexample.com/fn/closures/input_parameters.html
// A function which takes a closure as an argument and calls
// it. The closure takes no input and returns nothing.
fn apply<F>(f: F) where
F: FnMut() {
// ^ TODO: Try changing this to 'Fn' or 'FnMut'.
f()
}
// A function which takes a closure and returns an 'i32'.
fn apply_to_3<F>(f: F) -> i32 where
// The closure takes an 'i32' and returns an 'i32'.
F: Fn(i32) -> i32 {
f(3)
}
fn main() {
let greeting = "hello";
// A non-copy type.
let mut farewell = "goodbye".to_owned();
// Capture 2 variables: 'greeting' by reference and
// 'farewell' by value.
let diary = || {
// 'greeting' is by reference: requires 'Fn'.
println!("I said {}.", greeting);
// Mutation forces 'farewell' to be captured by
// mutable reference. Now requires 'FnMut'.
farewell.push_str("!!!");
println!("Then I screamed {}.", farewell);
println!("Now I can sleep. zzzzz");
// Manually calling drop forces 'farewell' to
// be captured by value. Now requires 'FnOnce'.
//drop(farewell);
};
// Call the function which applies the closure.
apply(diary);
let double = |x| 2 * x;
println!("3 doubled: {}", apply_to_3(double));
}
error: cannot borrow immutable local variable `f` as mutable
I commented out the drop, so the closure is of type FnMut and changed the annotation from FnOnce to FnMut. What am I missing?