This tutorial provides a quick start guide to installing Rust and running your first Rust project using both rustc
and cargo
.
Lesson | Description |
---|---|
1️⃣ | Hello World – Your first Rust program with fn main() and println!() |
2️⃣ | My Calculator – A modular Rust CLI calculator using lib.rs , main.rs , and bin/*.rs |
Open your terminal and run the following:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the prompts. Then activate Rust for your current session:
source $HOME/.cargo/env
Verify installation:
rustc --version
cargo --version
echo $PATH
cargo
Create a new project:
cargo new get-dependencies
cd get-dependencies
Build the project:
cargo build # Compiles and creates an executable
cargo check # Checks code without building an executable
Edit the default source file:
cd src
Open main.rs
and add:
fn main() {
println!("Hello, world!");
}
Run your code:
cargo run
rustc
(Direct Compilation)If you want to manually compile without cargo
:
main.rs
:fn main() {
println!("Hello, world!");
}
rustc main.rs
./main
my_game
)To create a new Rust project called my_game
, run:
cargo new my_game
cd my_game
cargo run
Command | Description |
---|---|
cargo new <name> |
Create a new Rust project |
cargo build |
Compile the project |
cargo check |
Check code for errors without building |
cargo run |
Compile and run the project |
rustc main.rs |
Compile a standalone Rust file |
./main |
Run compiled Rust binary |