Rust: basics

Rust: basics


2 min read


The Rust Programming Language

To install the Rust toolchain, you can visit the official website.


cargo - the package manager

cargo doc --open - generates the documentation for all of your dependencies and opens it in the browser

clippy - the linter

rustc - the compiler

rustup - the toolchain manager


In Rust, variables are immutable by default. You can define variables using the let keyword and make them mutable via the mut keyword. Additionally, you can define constants using the const keyword.

💡 Difference between variables and constants

  • constants are not mutable by default, they are always immutable

  • constants type must be annotated

  • the value of a constant can’t be an expression that can only be computed at runtime. Only constant expressions.

Example of variable definition:

let my_variable = String::New(); // unmutable variable
let mut: i32 = 43; // mutable variable
const THREE_HOURS_IN_SECONDS = 60 * 60 * 3;

You can also set variable values using an if statement.

Example of setting variable value conditionally:

let condition = true;
let number = if condition { 5 } else { 6 };

Shadowing is a technique that allows you to transform the value of a variable without making it mutable. It involves reusing the same variable name

Example of shadowing:

let my_string = "hello_rust";
let my_string = my_string.len();


The Cargo.toml file is used to store package information and dependencies. Rust's dependency manager understands semantic versioning (SemVer) and sticks to the minor version of the dependency by default.

It's important to note that you need to manually add the dependency to the Cargo.toml file and then run cargo build to download all the dependencies. By default, Cargo will use the versions of dependencies specified in the Cargo.lock file. To update the dependencies, you can use the cargo update command.

💡 example: rand = 0.8.0 means ^0.8.0 , meaning it will not update to 0.9.0 when it comes out