Using rustup
(recommended)
To compile a Rust program, you'll need to use the rustc
compiler or cargo to run
/build
a program. You can install Rust on UNIX-based systems such as macOS or Linux using the curl and sh
commands to download and run the rustup script:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Alternatively, you can install Rust using the apt
package manager:
sudo apt install rustc
Standalone direct download
The official Rust standalone installers contain a single release of Rust, and are suitable for offline installation.
They come in three forms: tarballs (extension .tar.gz), that work in any Unix-like environment, Windows installers (.msi), and Mac installers (.pkg).
These installers come with rustc, cargo, rustdoc, the standard library, and the standard documentation, but do not provide access to additional cross-targets like rustup does.
To download the standalone installer for your OS, visit Installation Methods - Standalone installers and select the target form for your OS and desired channel (stable, beta, and nightly).
Hello world
Create a file called main.rs
and create a new function called main
. In the main
function's body, use a println!
macro to write a "Hello World!"
string to the console.
fn main() {
println!("Hello World!");
}
After saving the file, run the following in your terminal and press enter:
rustc main.rs
When the program has finished compiling, the output is an executable file called main
, which we would run using:
./main
Which would give us an output of Hello World!