logoalt Hacker News

bobbylarrybobbylast Friday at 8:35 PM1 replyview on HN

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.


Replies

bryanlarsenlast Friday at 11:34 PM

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.

show 1 reply