I saw this code in the wild:
fields.sort_by_key(|&(_, ref field)| field.tags().into_iter().min().unwrap());
let fields = fields;
What does the let fields = fields;
line do? Why is it there?
I saw this code in the wild:
fields.sort_by_key(|&(_, ref field)| field.tags().into_iter().min().unwrap());
let fields = fields;
What does the let fields = fields;
line do? Why is it there?
The statement
let var = var;
makesvar
immutable and bound to its current value.fields
was declared asmut
earlier.It makes
fields
immutable again.fields
was previously defined as mutable (let mut fields = …;
), to be used withsort_by_key
which sorts in-place and requires the target to be mutable. The author has chosen here to explicitly prevent further mutability."Downgrading" a mutable binding to immutable is quite common in Rust.
Another common way to do this is to use a block expression: