Rust aims to provide a more ergonomic programming experience to all developers while helping you to write less insecure and faulty code (especially when it comes to low-level code and concurrency) and producing fast and stable programs.
Installation
The easiest way to get set up is to use the official toolchain installer, rustup.
In Linux and MacOS/OSX
Execute the following command in the CLI:
| |
If you have an installation of Rust already on your system, be sure to first uninstall it, e.g. if on MacOS with
brewthenbrew remove rust
If on MacOS/OSX, ensure that you have a C compiler installed with
xcode-select --install
In Windows
The simplest option is to download the installer here_x32 or here_x64 and run it.
Installation confirmation
In order to confirm that the rustc compiler is installed and ready for action, run:
| |
You should see something like:
| |
Writing your first Rust program
In keeping with programming tradition we shall hack together a quick program that prints “Hello, World!” to standard output.
Fire up your favourite editor/IDE (I’m hoping you choose Vim 😬), type up the following bit of code and save it as main.rs:
| |
The basics of main.rs
fnis the keyword to define a function in Rust and in this case, i.e. themainfunction, it is the entrypoint into the program.- functions need to be wrapped with braces i.e.
{} - code should be indented with 4 spaces, not tabs.
- macro calls look like function calls except for the exclamation mark in between the name and the opening parenthesis e.g.
println()vsprintln!() - lines end with a semi-colon.
Compiling and running
Compiling and running your Rust programs is a two-step process. To compile the program, run:
| |
And to run the binary:
| |
These can be combined with the AND/&& operator in your shell thusly:
| |
Note: if you’re on Windows, omitting the
./should work 😬
You should see the following output:
| |
In the next article we’ll start diving in to the details 😬