How to Create Your First Rust Program
What the reader will be able to do: Write, compile, and run a simple "Hello, World!" program in Rust.
Prerequisites
- Rust compiler installed (via
rustup) - Basic understanding of command-line tools
- A text editor (e.g., VS Code, vim)
Steps
-
Install Rust
If you haven't installed Rust, run:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shThis installs
rustc(the compiler) andcargo(the package manager). -
Create a new project
Run:cargo new hello_rust cd hello_rustThis creates a directory
hello_rustwith a basic project structure. -
Write the program
Opensrc/main.rsin your editor and replace its contents with:fn main() { println!("Hello, world!"); } -
Compile and run
Run:cargo runExpected output:
Hello, world! -
Build the executable
Run:cargo buildThis creates a binary in
target/debug/hello_rust.
Pitfalls
- Forgetting to run
cargo runinstead ofrustcdirectly - Not installing Rust via
rustup(leads to missing dependencies) - Confusing
main.rswithlib.rs(usemain.rsfor executables)
To go further
- Learn about
cargodependencies inCargo.toml - Explore Rust's ownership model in advanced tutorials
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|