Rust
Rust is relatively new, statically typed, non garbage collected “systems” programming language. The most unique aspect about Rust compared to similar languages (such as C, C++) is Rusts ownership model (more on this below), which aims to completely eliminate specific memory bugs and race conditions that are common in similar languages.
Getting Started
Install Rustup
The easiest way to get started with Rust is to install Rustup, a Rust installer and version management tool.
On a Linux, macOS or other UNIX-like OS:
This will typically provide you with a prompt with options --- accept the defaults to install.
On Windows:
Download rustup-init.exe
from the Rust website and run the installer.
If you are using the Windows Subsystem For Linux (WSL), you can use the instructions above for UNIX-like OS’s.
Create A New Project With cargo
cargo
should setup a basic project for you. You can now run the template by doing the following:
Ownership
The most unique aspect of the Rust language is it’s ownership model. This ownership system combines ownership, borrowing and copying concepts and relies heavily on zero-cost (no run-time influence) compiler checks to implement it. The system is designed to prevent common software issues relating to concurrency and data races.
Lifetimes Of References
Every reference in Rust comes with a lifetime, although in many cases Rust will let you omit the lifetime syntax and the references lifetime will be implied by the context. Lifetimes are specified by '<identifier>
, e.g. 'a
would read the “lifetime of a
”, and 'xyz
the “lifetime of xyz”.
You can read more about the ownership system in the official Rust book.
Casting
Basic type casting is done is Rust with the as
operator:
Printing To stdout
Rust provides you with the println!()
macro to perform basic printing to stdout
. It supports a C-like variable length argument syntax with the string as the first parameter, and then successive parameters being the replacement variables. While C uses %s
,%u
, e.t.c to denote locations for replacement, Rust uses {}
:
You do not have to import any module to call println!()
.
Source Code
The source code for Rust is maintained on GitHub at https://github.com/rust-lang/rust.
Online Editor
You can write and run Rust test code online at the Rust playpen.
Embedded Platforms
Rust is beginning to be used on embedded platforms (however, it is still in a mostly experimental manner). There is discussion of the suitability of rust for an embedded platform at Atomic Object.
zinc.rs is an attempt to write an ARM stack (similar to CMSIS or mbed in functionality, which are both written in C) using the rust programming language.
IDE Support
The Rust (rls) extension for Visual Studio Code adds Rust code completion, jump to definition, peek definition, find all references, symbol search, documentation on hover, code formatting, refactoring, error squiggles, snippets, build tasks and more.
Containers
HashMap
Known in other languages as a map, dictionary (Python).
To add a value use the insert()
method. The following example adds the value 2
to the key "mykey"
.
To retrieve a value at a certain key, use the get()
method:`
Dealing With Unrecoverable Errors
You can cause the current thread to panic with the panic!()
macro. This will cause the current thread to terminate. If you happen to be on the main application thread, this will cause the application to exit:
which will produce output similar to: