Rust by example - Getting Started: Basic Tools

1 minute read


Getting Started: Basic Tools

:information_source: Note: Read the offical docs to get the most updated information.

Install

Follow rustup instructions in rust-lang.org/tools/install

$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
$ rustc --version
## update
$ rustup update

In the Rust development environment, all tools are installed to the ~/.cargo/bin directory, and this is where you will find the Rust tools. rustup will include this directory to the PATH environment variable.

Hello World

Rust files always end with the .rs extension.

fn main() {
    println!("Hello, world!");
}

Notes about the code:

  • fn is how we declare a function in Rust, the parentheses () indicate there are no parameter and the function body is wrapped in {}.
  • main is the name of a special funtion: it is always the first code that runs in every executable Rust program.
  • The line println!() uses a macro. If it had called a function instead, it would be entered as println (without the !).
  • The line ends with a semicolon ; which indicates that this expression is over.
  • Rust style is to indent with four spaces, not a tab.

Compile and run

$ rustc main.rs
$ ./main
Hello, world!

Similar to gcc, rustc will produce a binary file called main.

Updated:

Leave a Comment