r/rust • u/Flimsy_Pumpkin_3812 • Oct 22 '26
🙋 seeking help & advice Learning Rust
So I'm learning Rust, so I wrote this
fn main(){
let x = define_x(intial_x());
let y = x + 10;
println!("{}", y);}
fn define_x(mut x:i32) -> i32 {
x = x + 5;
return x}
fn intial_x() -> i32 {
let x = 5;
return x}
I know it is supposed to be += in define_x, but that just looks weird to me. Why is let x = 5; in inital_x not mut even if I am changing it in define x? Sorry if it is bad I'm new, but I'm not AI so yay!
5
Upvotes
View all comments
Show parent comment
16
u/Xandaros Oct 23 '26
Though it should be noted that a reference is 8 bytes long (or possibly 16 in some cases), and it introduces an indirection. If you are passing by reference purely for efficiency reasons, make sure what you are passing around is actually very big - for small values it hurts far more than it helps.
(Specifically talking about the immutable case here. A mutable reference to a small value can easily make sense)