Avatar ·

What is the difference between immutable variables and constants in Rust?

Hello! I started learning Rust and got confused about a fundamental concept. The documentation says that variables are immutable by default, and you need to use `mut` to change them. But there is also the `const` keyword. What is the difference between them?\n\nI tried writing this code:\n\n```rust\nconst MAX_POINTS: u32 = 100_000;\nlet x = 5;\nx = 6; // Error!\n```\n\nAnd it seems like both `MAX_POINTS` and `x` cannot be changed after initialization. But the compiler complains about `x = 6`, while it doesn't about the constant. Why? And if I can declare an immutable variable using `let`, then why do constants even exist? In what cases should I use `const`, and in what cases `let`?\n\nI also noticed that constants always need a type annotation, while variables don't always. Is this related to their nature? I would be grateful