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:
Create a New Project
To create a new Rust project, run:
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:
Variables
Rust variables are immutable by default. To make a variable mutable, add the mut
keyword before the variable name.
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
Type | Size | Description |
---|
i8 | 1 byte | Signed 8-bit integer |
i16 | 2 bytes | Signed 16-bit integer |
i32 | 4 bytes | Signed 32-bit integer |
i64 | 8 bytes | Signed 64-bit integer |
u8 | 1 byte | Unsigned 8-bit integer |
u16 | 2 bytes | Unsigned 16-bit integer |
u32 | 4 bytes | Unsigned 32-bit integer |
u64 | 8 bytes | Unsigned 64-bit integer |
isize | size of pointer | Signed integer of the size of a pointer |
usize | size of pointer | Unsigned integer of the size of a pointer |
f32 | 4 bytes | 32-bit floating point number |
f64 | 8 bytes | 64-bit floating point number |
char | 4 bytes | Unicode scalar value |
bool | 1 byte | Boolean 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
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}");
}
|
Print Output
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}");
|