Assignments and precedence
Quick answer
Assignment stores a new value in an assignable location. Compound assignment such as += updates a mutable binding. Operator precedence determines how an expression is grouped when parentheses are absent; multiplication binds more tightly than addition.
Group the calculation explicitly
count += 2 increases a numeric mutable count by two. An assignment expression has unit type (), rather than the assigned numeric value. Parentheses make intended grouping explicit, especially in formulas with several operations. Precedence describes grouping, not a general rule for side-effect order.
fn main() { let mut total = 10; total += 4; total *= 2; println!("{total}");}Expected stdout (not executed):
28The updates happen in separate statements: first 14, then 28.
fn main() { println!("{}", 2 + 3 * 4); println!("{}", (2 + 3) * 4);}Expected stdout (not executed):
1420Parentheses change the grouping of the addition.
Common mistake
Do not chain assignments expecting the assigned number as the result. Use separate statements and keep formulas readable.
Try it yourself
Calculate the price of three items at four units each, plus a two-unit delivery fee. Then group the formula incorrectly and explain the difference.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Assignments and precedence — practice quiz
Choose one answer per question, then review the explanation.
What is the result of `8 + 2 * 5`?
What is n after `let mut n = 9; n -= 2; n *= 3;`?
What is the type of the assignment expression `n = 8` when n is mutable i32?