What you'll learn: Your first Rust program —
fn main(),println!(), and how Rust macros differ fundamentally from C/C++ preprocessor macros. By the end you'll be able to write, compile, and run simple Rust programs.
fn main() {
println!("Hello world from Rust");
}
fn keywordmain()println! looks like a function, but is actually a macro. Macros in Rust are very different from C/C++ preprocessor macros — they are hygienic, type-safe, and operate on the syntax tree rather than text substitutionevcxr_repl for an interactive Rust REPL (like Python's REPL, but for Rust):cargo install --locked evcxr_repl
evcxr # Start the REPL, type Rust expressions interactively
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shrustc is the standalone compiler, but it's seldom used directlycargo is the Swiss Army knife and is used for dependency management, building, testing, formatting, linting, etc.stable, beta and nightly (experimental) channels, but we'll stick with stable. Use the rustup update command to upgrade the stable installation that's released every six weeksrust-analyzer plug-in for VSCodecrates.io.cargo tool automatically handles the downloading of crates and their dependencies. This is conceptually equivalent to linking to C-librariesCargo.toml. It also defines the target type for the crate: standalone executable, static library, dynamic library (uncommon)my_project/
|-- Cargo.toml # Project configuration (like package.json)
|-- Cargo.lock # Exact dependency versions (auto-generated)
|-- src/
| |-- main.rs # Main entry point for binary
| |-- lib.rs # Library root (if creating a library)
| `-- bin/ # Additional binary targets
|-- tests/ # Integration tests
|-- examples/ # Example code
|-- benches/ # Benchmarks
`-- target/ # Build artifacts (like C's build/ or obj/)
|-- debug/ # Debug builds (fast compile, slow runtime)
`-- release/ # Release builds (slow compile, fast runtime)
helloworldcargo new helloworld
cd helloworld
cat Cargo.toml
cargo run will compile and run the debug (unoptimized) version of the crate. To execute the release version, use cargo run --releasetarget folder under the debug or release folderCargo.lock in the same folder as the source. It is automatically generated and should not be modified by hand
Cargo.lock later