Skip to main content

Rust

About​

Rust is a multi-paradigm programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but can guarantee memory safety by using a borrow checker to validate references. Rust achieves memory safety without garbage collection, and reference counting is optional. (Wikipedia, 2020)

Getting started​

Rustup: the Rust installer and version management tool​

The primary way that folks install Rust is through a tool called Rustup, which is a Rust installer and version management tool.

  • Install Rust
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Update Rust
    rustup update

Cargo: the Rust build tool and package manager​

When you install Rustup you’ll also get the latest stable version of the Rust build tool and package manager, also known as Cargo. Cargo does lots of things:

  • build your project with cargo build
  • run your project with cargo run
  • test your project with cargo test
  • build documentation for your project with cargo doc
  • publish a library to crates.io with cargo publish

To test that you have Rust and Cargo installed, you can run this in your terminal of choice:

cargo --version

Create a new project​

cargo new my_project

Clippy: the Rust linter​

A collection of lints to catch common mistakes and improve your Rust code.

How to setup a workspace​

How to work with Rust in IntelliJ IDEA​

Debugging with Macro std::dbg​

Prints and returns the value of a given expression for quick and dirty debugging. Use dbg!(..) see https://doc.rust-lang.org/std/macro.dbg.html

let a = 2;
let b = dbg!(a * 2) + 1;
// ^-- prints: [src/main.rs:2] a * 2 = 4
assert_eq!(b, 5);

Resources​

Compile error?​

If you are facing an error like that on new MacOS version.

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

It means that you need to install XCode command line, open a Terminal and run this command:

xcode-select --install

Note: If you want to download and install Command Line tools manually, it can be downloaded from: https://developer.apple.com/download/more/