The rules of writing types and expressions in Rust are much simpler than in C++. I don't really know what exactly confuses you in the code you provided. It would help if you asked more specific questions.
Regarding mut
, it's important to know that it has two distinct, not really connected meanings. First, it's a part of the exclusive reference syntax: &mut target
(as opposed to shared reference syntax &target
). This can be used both in types (e.g. &mut Box<u32>
type is an exclusive reference to Box<u32>
) and in expressions (e.g. &mut Box::new(5)
expression is an exclusive reference to Box::new(5)
). Forgetting to take a reference to something usually results in type errors, so the compiler messages should help you with that.
The second meaning of mut
is that it marks a binding as mutable. For example, it can appear in this role in a local variable declaration:
let mut b = 5;
Or in a pattern:
if let Some(mut x) = something { /*...*/ }
Or in an argument declaration:
fn function_name(mut arg_name: u32) { /*...*/ }
Please note that in the second meaning, mut
has no impact on type of the binding or values of any expressions. It only allows you to create a mutable reference to the value of the binding, which is not allowed otherwise. So you may or may not need to declare a variable with mut
, depending on how you use the variable later. To make it easy, you can just make a binding mut
when the compiler asks you to, and remove mut
when the compiler complains about it (note that both of these errors/warnings are not type errors).