Notes taken from “The Rust Programming Language” book: Link to Book
Setup and Installation
To install Rust on MacOS or Linux, enter the following command in your terminal:
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
If you’re on Windows, look it up on Google…
To update enter into terminal:
rustup update
If one just wants to create a Rust file without using Cargo, they may create a file example.rs and compile using:
rustc example.rs
./main
Most Rust projects will however use Cargo. Cargo comes preinstalled with Rust; you can check your Cargo version with:
cargo --version
To create a project with Cargo, run:
cargo new project_name
You will see that a new folder appears with the project_name and that is contains a couple of other folders and a Cargo.toml file which by default includes:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
Inside of the src folder, you will see a main.rs file that will be the main file for the particular program/application that you are building.
Inside of main.rs, the default will always be a “Hello world!” program.
fn main() {
println!("Hello World");
}
To build and run a program using Cargo, run the following in the terminal:
cargo build
cargo run
To build with optimizations, run:
cargo build --release
Adding Crates
In the Cargo.toml file, add other crates needed under dependencies:
[dependencies]
rand = "0.8.5"
When building, the dependency will automatically be added.
To update a Crate to a newer version, run the following in the terminal:
cargo update
Writing Basic Functions
You can use the standard i/o library in Rust by using the following:
use std::io;
Main is always declared by writing:
fn main() {
println!("Hello");
}
Other functions can be written like the following:
fn other_function(variable: i32) -> i32 {
println!("The value passed was {variable}");
variable // return values are typically passed implicitly in rust
// can also be returned by using: return variable
}
They are called by main in a similar fashion to other programming languages:
fn main() {
other_function(5);
}
Declaring and Using Variables
Declaration of variables:
let variable = 10;
let mut mutableVariable = 5;
let mut mutableTypedVariable = String::new();
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
let tup: (i32, f64) = (500, 5.2)
Feeding user input into variable:
io::stdin().read_line(&mut mutableTypedVariable).expect("Failed to read line")
// The expect allows failure to happen without compilation errors
Using placeholders to print variables:
let x = 5;
let y = 10;
println!("x = {x} and y + 2 = {}", y + 2);
Standard comparators can be used to compare values as well as std::cmp::Ordering
Declaring Arrays
let months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
// Standard declaration
let a: [i32; 5] = [1, 2, 3, 4, 5];
// Declaring all numbers to be 32 bit integers
Accessing elements is similar to other programming languages:
let array = [1, 2, 3];
let firstEl = array[0];
If/Else Statements
fn main() {
let number = 3;
if number != 5 {
println!("Not 5");
} else if number % 5 != 0{
println!("Not Divisble by 5");
} else {
println!("Not a factor of 5");
}
}
Loops
loop {
// put code here
if condition {break;}
}
while given_condition_not_met {
// put code here
}
for i in (1..3) {
// put code here
}
for j in (1..3).rev() {
// put code here
}
for num in numbers {
// put code here
}