Tainted \\ Coders

Rust Syntax

Last updated:

This is a collection of interesting/confusing parts of rust syntax.

Declaring parameters

error[E0384]: cannot assign twice to immutable variable `value`
   --> src/second.rs:100:13
    |
99  |         list.peek_mut().map(|&mut value| {
    |                                   -----
    |                                   |
    |                                   first assignment to `value`
    |                                   help: make this binding mutable: `mut value`
100 |             value = 42
    |             ^^^^^^^^^^ cannot assign twice to immutable variable          ^~~~~

The compiler is complaining that value is immutable, but we pretty clearly wrote &mut value; what gives?

It turns out that writing the argument of the closure that way doesn’t specify that value is a mutable reference. Instead, it creates a pattern that will be matched against the argument to the closure.

|&mut value| means “the argument is a mutable reference, but just copy the value it points to into value, please.” If we just use |value|, the type of value will be &mut i32.

Source

Iterators

Iterator<Item = &'g Lime>> says:

“I can iterate over items that last as long as 'g