Home Rust
Post
Cancel

Rust

1
2
3
fn main() {
    println!("Hello, World!");
}

Cargo Package Manager

Cargo is the Rust package manager. It is used to compile, test, and run Rust projects. Cargo is also used to manage dependencies.

Installation

Cargo is included with the Rust installation. To check if Cargo is installed, run:

1
cargo --version

Create a New Project

To create a new Rust project, run:

1
cargo new hello_world

This will create a new directory called hello_world with the following structure:

1
2
3
4
hello_world
├── Cargo.toml
├── src
│   └── main.rs

Add Package Dependencies

To add a package dependency, open the Cargo.toml file in the project directory and add the following line:

1
2
[dependencies]
rand = "0.8.4"

This will add the rand package as a dependency to the project.

Build and Run

To build and run the project, navigate to the project directory and run:

1
cargo run

Variables

Rust variables are immutable by default. To make a variable mutable, add the mut keyword before the variable name.

1
let mut x = 5;

Data Types

Rust is a statically typed language, which means that the data type of a variable is known at compile time. However, the compiler can usually infer the data type based on the value and how it is used.

Scalar Types

TypeSizeDescription
i81 byteSigned 8-bit integer
i162 bytesSigned 16-bit integer
i324 bytesSigned 32-bit integer
i648 bytesSigned 64-bit integer
u81 byteUnsigned 8-bit integer
u162 bytesUnsigned 16-bit integer
u324 bytesUnsigned 32-bit integer
u648 bytesUnsigned 64-bit integer
isizesize of pointerSigned integer of the size of a pointer
usizesize of pointerUnsigned integer of the size of a pointer
f324 bytes32-bit floating point number
f648 bytes64-bit floating point number
char4 bytesUnicode scalar value
bool1 byteBoolean value

Arrays

1
let a = [1, 2, 3, 4, 5];
1
2
3
4
a[0] = 6;
a.push(7);
a.pop();
a.remove(2);

Hash Maps (Dictionaries)

1
2
3
let mut scores = HashMap::new();
scores.insert("Blue", 10);
scores.remove("Blue");

IO

Read Input

To read input from the user, use the std::io module.

1
2
3
4
5
6
7
use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    println!("You entered: {input}");
}

To print output to the console, use the println! macro.

1
println!("Hello, World!");  

Control Flow

if else Statement

1
2
3
4
5
if number % 2 == 0 {
    println!("The number is even");
} else {
    println!("The number is odd");
}

match Statement

1
2
3
4
5
6
match number {
    1 => println!("One"),
    2 => println!("Two"),
    3 => println!("Three"),
    _ => println!("Other"),
}

while Statement

1
2
3
4
5
let mut number = 3;
while number != 0 {
    println!("{number}");
    number -= 1;
}

loop Statement

1
2
3
loop {
    println!("Loop forever!");
}

Functions

1
2
3
fn another_function(x: bool) {
    println!("The value of x is: {x}");
}

Return Values

1
2
3
fn five() -> i32 {
    5 //as i32 (optional casting)
}

Expressions

1
2
3
4
5
let y = {
    let x = 3;
    x + 1
};
println!("The value of y is: {y}");
This post is licensed under CC BY 4.0 by the author.