Introduction
Rust is a relatively young language in the field of systems programming. The first stable version appeared in 2015, though it was announced back in 2010. It combines different programming paradigms, namely from functional, object-oriented, and concurrent programming. This means programmers from other languages will quickly find familiar elements, though they may also be intimidated by paradigms they don’t know yet.
Installation
The easiest way to install Rust is using rustup, the official Rust installer. To do this, enter the following curl command in your terminal:
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | shThis command downloads a script and starts the installation of the rustup tool, which in turn installs the latest version of Rust. Once the installation is complete, a message will appear: “Rust is installed now. Great!”
Now you’re almost ready to start programming. Next, make sure a linker is installed. This is usually included with a C compiler. In other words: you should install either GCC or Clang. On Ubuntu, it is sufficient to install the build-essential package:
sudo apt install build-essentialIf you use an openSUSE distribution, the following command will help:
sudo zypper install gccOn Fedora Linux, run the following command:
sudo dnf install curl gcc makeYou can verify that the C compiler was installed successfully by running:
gcc --versionAfter the Installation
Once Rust is installed, three new commands become available. It’s a good idea to run each of them with the --version option to verify the installation. (You should restart the terminal first.)
rustc --version
rustdoc --version
cargo --versionrustcis the Rust compiler.rustdocallows you to create documentation (in HTML format).cargois the central tool for compiling and managing packages.
Although you can userustcto compile directly, it’s generally better to usecargo.
Creating a New Rust Project
Use cargo to create a new project or package. The following command creates a package named “hello”:
cargo new helloThis generates a new folder with the same name. Inside, you’ll find the Cargo.toml file and a subdirectory called src. As the name suggests, this is where the code files go. Initially, it contains just one file: main.rs.
hello
|
|--Cargo.toml
|--src
|-- main.rsThe File main.rs
Let’s take a look at the main.rs file located in the src subfolder. First, change into the project directory using:
cd helloThen start Visual Studio Code with:
code .The main.rs file contains just three lines of code:
fn main() {
println!("Hello, world!");
}This is the main() function — introduced by the keyword fn — which uses the println!() macro (indicated by the exclamation mark) to print the text “Hello, world!”.
You can run this code in the terminal. In Visual Studio Code, you can open a terminal via:
> Terminal > New TerminalIn the terminal, run:
cargo runor simply:
cargo rThis will compile the code and run the program, displaying “Hello, world!”.
A new subdirectory named target is also created. Inside its debug subfolder, you’ll find the files generated by the compiler, including the executable hello.exe.
To remove all files created during the build process, use:
cargo cleanThis will delete the target directory.
To perform only the build process (without running the program), use:
cargo buildAnother useful command is:
cargo checkThis checks whether your code contains any warnings or errors, i.e., whether it is compilable. No executable is created during this process.
Now you know how to get started with Rust. If you’d like to learn more, you should definitely take a look at the official documentation.
RustRover
RustRover is a high-quality development environment for Rust, available for free in its Community Edition. It can be downloaded here. RustRover was developed by JetBrains, who previously offered a Rust plugin for their IDEs.