Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can also de-mut-ify a variable by simply shadowing it with an immutable version of itself:

let mut data = foo(); data.mutate(); let data = data;

May be preferable for short snippets where adding braces, the yielded expression, and indentation is more noise than it's worth.



Variable shadowing felt wrong for a while because it's considered verboten in so many other environments. I use it fairly liberally in rust now.


It helps that the specific pattern of redeclaring a variable just to change its mutability for the remainder of its scope is about the least objectionable use of shadowing possible.


That's not the only place I use shadowing though. I use it much more liberally.

For example I feel this is right:

    let x = x.parse()?;




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: