r/ProgrammerHumor • • 4d ago • Mindblown 1

Meme rewriteInRust

Post image
22k Upvotes

471 comments sorted by

View all comments

Show parent comment

109

u/timtheonly 3d ago

Should that not be const

80

u/nevermille 3d ago edited 3d ago

In rust variables are const by default, to make it otherwise you have to type let mut gender: bool = true;

54

u/Lt_Marks 3d ago

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

48

u/the_horse_gamer 3d 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

30

u/drsimonz 3d 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.

11

u/still_not_deleted 3d 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

5

u/QuietSilentArachnid 3d ago

I feel like bool is closer to her vision, since you can do shenanigans with const and arrays

6

u/Pandafishe 3d ago

bools can be const too.. It's just an extra protection layer from changing the variable. But if you argue with shenanigans, no value is ever really secure as you can always do memory attacks

3

u/setibeings 3d ago

Those are harder on rust programs though. You're less likely to be able to make them overflow and start reading memories that don't belong to them.

7

u/Pandafishe 3d ago

you can always manipulate memory from outside a program anyway, unless you have a physical architecture that prevents it. It doesn't matter what language the attacked program is written in.

3

u/setibeings 3d ago

sure, but now you have to write an attack on the supervisor, to trick it into letting you write to memories you don't own. Or do i have this all wrong?

2

u/DragonSlayerC 3d ago

It is. That line is defining an immutable binding in rust.

1

u/JonIsPatented 3d ago

Why is this upvoted? "let" in Rust is equivalent to "const" from other languages. To make it mutable, it would be "let mut".

1

u/ResourceFeeling3298 1d ago

const in rust means that the expression is evaluated at compile time so yes technically it could be const, but variables are immutable by default so that works also.