4 Comments
User's avatar
Yacob Cohen-Arazi's avatar

Can you please explain the Rust part? I thought everything, including a struct fields are immutable by default. see here: https://stackoverflow.com/questions/47748091/how-can-i-make-only-certain-struct-fields-mutable

Expand full comment
Taras Tsugrii's avatar

nope, it's the opposite - Rust has no field level mutability, which means that depending on the type of borrow all fields are either mutable or immutable. In other words, unlike C++, you cannot have a mutable variable with immutable fields or the the other way around. Yet another way to look at it is that Rust's borrows are like passing by & or const & in C++.

Expand full comment
Yacob Cohen-Arazi's avatar

OK. totally my bad. I did not read the SO question thoroughly. I looked at the first example and thought it is a valid code. thanks for the clarification!

Expand full comment
Taras Tsugrii's avatar

no worries and the examples on StackOverflow are correct - they show that the entire struct is mutable when borrowed using mut&, which is the same as T& in C++.

Expand full comment