Installing Rust
Rust is under active development, and some of its features are only available in experimental versions of the compiler. We may use some of these features. To get a compiler that supports them, run the rustup installer. Restart your editor so that it discovers the newly installed tools.
The Rust tools are also installed on stu
.
First Program
Now that we have a compiler, we need some source code to compile. This code defines a main
function that prints a message:
fn main() {
let target = "Pluto";
println!("Goodbyte, {}!", target);
}
Place this code in a file named main.rs
.
The compiler is named rustc
. We compile and run main.rs
with these two commands:
> rustc main.rs
> ./main
Goodbye, Pluto!
That said, many Rust developers do not invoke the compiler directly. They instead use Cargo, a build tool and dependency manager in one that is installed alongside the compiler. We make a new Cargo project with these shell commands:
> cargo new my-project
> cd my-project
Then we add our code to the default src/main.rs
and other files in that directory. Cargo will download any dependencies that haven't already been downloaded, build our project, and run it with this single command:
> cargo run
Goodbye, Pluto!
If we just want to compile but not execute the program, we run cargo build
.