Getting Started with Rust

In order to get started with Rust, it must first be installed. The process for installation varies depending on the operating system in use. Further details can be found here.

A Rust program can be written using any text editor, however, a code editor, or Integrated Development Environment, that supports Rust and includes syntax highlighting can be helpful.

Code Commenting

When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in Rust it needs to start with ‘//’, as shown below.

// This is a comment in Rust.

It is also possible to have comments that span multiple lines. Multi-line comments must start with ‘/*’ and end with ‘*/’.

/* This is 
a multi-line
comment
in Rust.*/

First Rust Program

The Rust code written below simply displays the words ‘Hello World!’ in a console or terminal window.

// Main function.
fn main() {

    // Display a message in the console.
    println!("Hello World!");

}

Every Rust program must have a 'main' function, as shown above. The code within the function must always be enclosed in curly brackets, which are sometimes referred to as curly braces.

In Rust, as in most languages, it has what are called 'reserved words', which have a specific purpose and cannot be used for anything else. In the above example, 'fn' is a reserved word that denotes a function.

A Rust program can be saved in a file with a '.rs' file extension, for example, 'main.rs'. Once this is done, it is ready to be compiled and run.

Compiling and Running a Rust Program

Compiling a Rust program is very similar across all operating systems. Firstly, navigate to where the program resides within a command prompt or terminal window, depending on the operating system being used, and type the following, then press enter.

rustc main.rs

Assuming that the program compiled successfully, it can then be run. The way this is done differs slightly depending on the operating system in use. For Microsoft Windows, type the following at the command prompt.

.\main.exe

For Linux and macOS, the following needs to be entered into a terminal window.

./main

Rust Projects

If a program contains more than one file and includes dependencies, it is a good idea to create a project. A dependency is a third-party library that your code depends on. When Rust is installed, in comes with a tool called Cargo, which can be used to both build and run your project, as well as manage any dependencies. The following command can be used to create a project called 'hello_project'.

cargo new hello_project

This creates a folder at the current location, with the same name as the project. Within this folder an 'src' folder has been created to house Rust source code files. A 'main.rs' file is also included that contains a sample hello world program.

From within the project folder, the project can be built using the Cargo 'build' command.

cargo build

The default build type is a debug build. The resulting program that can be run, can be found in the 'debug' folder, within the 'target' folder. Note that the name of the program is the same as the project name. This can be run as before.

A shortcut to building and running a project can be achieved using the Cargo 'run' command. This first builds the project, then, assuming this is successful, also runs it.

cargo run

In order to check code for its validity, but not build it, the Cargo 'check' command can be used.

cargo check

When the project is ready to be released, the 'release' option with the Cargo 'build' command must be used.

cargo build --release