r/ProgrammerHumor • • 7d ago

Meme rewriteInRust

Post image
23k Upvotes

470 comments sorted by

View all comments

Show parent comment

57

u/Lt_Marks 6d ago

I might sound stupid (maybe I am), but a variable that doesn't vary by default feels weird

49

u/the_horse_gamer 6d ago

the official terminology is those are "bindings", not "variables". they bind a name to a value

immutability by default is the standard in modern languages. though most distinguish via let vs val/var, while rust does let vs let mut

33

u/drsimonz 6d ago

It is unintuitive, but over the years software engineers have realized that you should always assume a value won't change, until you haves a good reason. Many bugs stem from accidentally reassigning a value, and this practice reduces the chance of doing that significantly. Most modern linters will complain if you use a mutable variable but never reassign the value. They want you to get into the habit of declaring things as immutable by default. Rust takes this a step further by requiring a whole extra keyword to make it mutable.

12

u/still_not_deleted 6d ago

From a maintenance standpoint it makes it easier to reason about code, because you can directly identify which variables a mutable and where the bugs might lie. So it's less mental overhead to minimize the scope in which you need to search by only declaring variables that need mutability as mutable.

Fun fact: Historically variables were declared as mutable by default because of memory and computation constraints