What is the difference between `mut` before a variable name and after the colon in Rust?
Hi everyone! I recently started learning Rust and encountered some confusion with variable declaration syntax. In tutorials, I see two different ways of using the `mut` keyword, and I can't understand the difference between them.
The first way — `mut` is placed before the variable name:
```rust
let mut x = 5;
```
The second way — `mut` is placed after the colon when specifying the type:
```rust
let x: mut i32 = 5;
```
I tried both options in my code, but the second one causes a compilation error. Here's an example I wrote:
```rust
fn main() {
let mut a = 10; // works
a += 5;
let b: mut i32 = 20; // error!
b += 10;
println!("a = {}, b = {}", a, b);
}
```
The compiler says something about "expected type, found keyword mut". I thought
No answers yet.
Log in to leave an answer