Installation
To install Rust:
- Linux/macOS: Run
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | shin the terminal. You may need to enter your password. If you encounter linker errors, install a C compiler (e.g., GCC/Clang on Linux, Xcode command-line tools on macOS). - Windows: Visit https://www.rust-lang.org/tools/install and follow the instructions. Install Visual Studio if prompted for a linker and libraries.
Verify installation with rustc --version. Update with rustup update; uninstall with rustup self uninstall.
Hello, World!
To create and run a simple "Hello, world!" program:
- Create a project directory (e.g.,
mkdir ~/projects/hello_worldon Linux/macOS). - Create
main.rswith:
fn main() {
println!("Hello, world!");
}
- Compile:
rustc main.rs. - Run:
./main(Linux/macOS) or.\main.exe(Windows). Output: Hello, world!
Introduction to Cargo
Cargo is Rust's build system and package manager. Verify with cargo --version.
Create a project: cargo new hello_cargo. This generates Cargo.toml and src/main.rs.
Build: cargo build (executable in target/debug/).
Run: cargo run (builds and runs in one step).
Check compilation: cargo check. For release: cargo build --release.
You Try
Edit the code below to print a different message, then click Run to compile and execute it.
fn main() {
println!("Hello, world!");
}