Efficient, secure, performant concurrent systems programming that compiles to machine running byte-code
Overview
rust-lang.org is the home page for the language.
NOTE: Content here are my personal opinions, and not intended to represent any employer (past or present). “PROTIP:” here highlight information I haven’t seen elsewhere on the internet because it is hard-won, little-know but significant facts based on my personal research and experience.
According to [1]Wikipedia, the Rust language begun in 2006 as a personal project by Graydon Hoare while a Mozilla employee, before he went to Apple. Rust is open-sourced (MIT license) since July 7, 2010. Rust 1.0 was released on May 15, 2015.
Rust maintainers have a 6-week rapid release process even though they support a large number of platforms.
In August 2020, Mozilla laid off most of the Rust team (along with completely disbanding the Servo parallel browser team). The event raised concerns about the future of Rust. So in Feburary 2021, the “Rust Foundation” was formed with (AWS, Huawei, Google, Microsoft, and Mozilla) all who use Rust as a systems programming language:
- TensorFlow and ONNX libraries use Rust’s FFI to wrap underlying C implementations.
- Linkerd creator Oliver Gould, says the Future of the Cloud will be Built on Rust CNCF
- Amazon Web Services (AWS) has used Rust since 2017 for its serverless computing offerings, AWS Lambda and AWS Fargate. With that, Rust has gained further inroads. The company has written the Bottlerocket OS and the AWS Nitro System to deliver its Elastic Compute Cloud (EC2) service.[3] See “How our AWS Rust team will contribute to Rust’s future successes,” http://mng.bz/BR4J
- Google, on April 6, 2021, announced support for Rust within Android Open Source Project as an alternative to C/C++. Google develops parts of Android, such as its Bluetooth module, with Rust. Rust is also used for the crosvm component of Chrome OS and plays an important role in Google’s new operating system, Fuchsia. http://mng.bz/ryOX
- Microsoft writes components of its Azure platform, including a security daemon for its Internet of Things (IoT) service, in Rust. See https://github.com/Azure/iotedge/tree/master/edgelet.
- Facebook uses Rust to power Facebook’s web, mobile, and API services, as well as parts of HHVM, the HipHop virtual machine used by the Hack programming language. See “HHVM 4.20.0 and 4.20.1,” https://hhvm.com/blog/2019/08/27/hhvm-4.20.0.html.
Rust uses LLVM to generate a language compiler/debugger, so performance improvements in LLVM would also benefit Rust.
Why Rust?
Rust was rated the “most loved” among all programming languages in the 2020 StackOverflow survey of developers (ahead of Python, TypeScript, Kotlin, etc.).
Rust was #26 on the TIOBE index of programming languages, based on “the number of skilled engineers world-wide, courses and third party vendors. Popular search engines such as Google, Bing, Yahoo!, Wikipedia, Amazon, YouTube and Baidu are used to calculate the ratings.”.
Like C, C++:
- Rust does not use a garbage collector because objects in Rust are discarded when the last “owner” goes out of scope.
Like C, C++, Java:
- Rust uses // for comments and ignored code between /* and */
- Rust uses //! to precede comments about the program file.
- In practice /// are comments that apply to a particular line below the comment.
- Rust uses semicolons to end each sentence.
Unlike other languages:
- which can have several variables point to the same memory, which can cause parallel and concurrency issues, Rust has a clone method.
- “&” in front of a variable designates “borrowing”
- Rust provides assert and cargo test commands for #[test] or #[should_panic]
- rustdoc command generates HTML documentation (like JavaDoc) without installing additional tools
Rust is used to write performance intensive, highly-concurrent code, with predictable tail latencies.
- Variables are immutable (can’t be changed) unless marked as “mut” (mutable).
- The lifetime of several variables that exist together are marked with: &’z Person and Person<‘z>
Thus, Rust can power performance-critical services, run on embedded devices, and easily integrate with other languages. “Rust enables low-level control without giving up high-level conveniences.”
Unlike Python & Go:
- Rust compiler issues errors when garbage situations are detexted.
- Rust eliminates memory issues (null and dangling pointers, data races, etc.). Thus:
- Parallel processing is a breeze with Rust Lifetimes references
- Rust’s rich type system and ownership model guarantees memory-safety and thread-safety — enabling you to eliminate many classes of bugs at compile-time.
- Rust has no runtime Rust does not use an intrepreter.
Like C:
- Developers manage memory in code.
Like Go:
- Rust compiles to machine running byte-code rather than a level of abstraction of a virtual machine.
Like shell scripts:
- Rust enables creation of CLI tools. See the gallary of CLIs built using Rust
Like Python:
- Rust can infer a data type by the value assigned.
Unlike Python, JavaScript:
- Rust has an integrated package manager and build tool: Cargo.
- Rust has smart multi-editor support with auto-completion and type inspections, and an auto-formatter.
- Rust does not have – and ++ operators.
- Rust raises “non-exhaustive patterns” compiler errors if match and if logic do not consider all possible values.
Rust catches concurrency programming mistakes.
Videos comparing languages:
- Rust vs Go: Which is best? THE Definitive Answer
- Rust Vs Go Panel - Miriah Peterson, Michael Snoyman, Jonathan Turner, Steve Francia RustLab Conference
- Rust vs Go - Which is Better and Why? by Federico Terzi
- Virtual Lunch & Learn: Why you want to choose Rust for your next web service! by CodeControl
Social ecosystem
Rust developers are called “Rustlings” or “Rustaceans”.
Rust is named after the rust family of parasitic fungi causing plant diseases with its brown spores, yet there is a
Rust Belt Rust: conference held in the “Rust Belt” of the U.S.
RustFest: Europe’s @rustlang conference
VIDEO: RustConf2021 YouTube mix
RustCon Asia
Rust LATAM
Oxidize Global
Install locally
-
To get the version of the Rust compiler on your machine:
rustc --version
At time of writing:
rustc 1.25.0 (84203cac6 2018-03-25)
https://www.rust-lang.org/tools/install
-
It’s available in Homebrew on MacOS:
brew info rust
rust: stable 1.55.0 (bottled), HEAD Safe, concurrent, practical language https://www.rust-lang.org/ Not installed From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/rust.rb License: Apache-2.0 or MIT ==> Dependencies Build: cmake ✘, ninja ✘, python@3.9 ✔ Required: libssh2 ✘, openssl@1.1 ✔, pkg-config ✔ ==> Options --HEAD Install HEAD version ==> Analytics install: 32,304 (30 days), 87,338 (90 days), 284,700 (365 days) install-on-request: 18,508 (30 days), 46,834 (90 days), 155,109 (365 days) build-error: 0 (30 days)
-
To install using Homebrew on MacOS:
brew install rust
==> Downloading https://ghcr.io/v2/homebrew/core/libssh2/manifests/1.10.0 ==> Downloading https://ghcr.io/v2/homebrew/core/libssh2/blobs/sha256:70c0928f2c ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh ==> Downloading https://ghcr.io/v2/homebrew/core/rust/manifests/1.55.0 ==> Downloading https://ghcr.io/v2/homebrew/core/rust/blobs/sha256:4486ea172caf9 ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh ==> Installing dependencies for rust: libssh2 ==> Installing rust dependency: libssh2 ==> Pouring libssh2--1.10.0.mojave.bottle.tar.gz 🍺 /usr/local/Cellar/libssh2/1.10.0: 184 files, 970.1KB ==> Installing rust ==> Pouring rust--1.55.0.mojave.bottle.tar.gz ==> Caveats Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary 🍺 /usr/local/Cellar/rust/1.55.0: 30,682 files, 742.4MB ==> Caveats ==> rust Bash completion has been installed to: /usr/local/etc/bash_completion.d
Upgrade
-
brew upgrade rust
Uninstall
-
brew uninstall rust
Alternately, if you used rustup to install:
-
rustup self uninstall
.gitignore for Rust
-
Obtain the folder:
git clone https://github.com/wilsonmar/rustlang-samples cd rustlang-samples
-
View the .gitignore file created by GitHub:
# will have compiled files and executables /target/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk
PROTIP: The target folder is where compiled assets are stored.
The Cargo.lock file?
Doug Milford (lambdavalley.com) uses Visual Studio Code (VSCode) IDE on YouTube:
- Rust intro trailer
- Rust setup for Windows
- Rust Hello World and Cargo.toml (without using cargo)
- Why Rust
- Rust Cargo.toml
- Rust lib vs main
- Rust Cargo and Rustup commands
- Rust println! and format! macros
- Rust Enumerations
- Rust Primatives (basic data)
- Rust strings vs str slices
- Rust Conditional Statements
- Rust Casting, Shadowing, Consts and Static
- Rust Functions and Procedures
- Rust Tuples
- Rust Rust Structs, Traits and Impl
- Rust Ownership and Borrowing shows coding for stack and heap (which houses collections which needs to grow)
- Rust Generics
- Rust lifetimes
VisualRust IDE?
Sample programs
The most thorough
https://github.com/TheAlgorithms/Rust
Hello World
-
Generate files for a Rust program:
cargo new hello
This creates a hello folder which contains a hello.rs file containing:
fn main() { println!("Hello, World!"); }
REMEMBER: “.rs” is the file extension for Rust program source files.
-
View file “Cargo.taml” (where toml stands for “Tom’s obvious minimal language”):
[package] hame="hello" version="0.0.1" authors=["John Doe <john.doe@gmail.com>] edition="2018"
edition refers to the year of the compiler to be used. 2018 is the lastest one as of this writing. (This should really be “Vintage”)
Compile
There are two ways of building a Rust program. One is cargo referencing a .taml file, and another is using the rustc command:
rustc hello.rs
On MacOS & Linux, compilation creates a “hello” file (with no file extension).
On Windows, compilarion creates a “hello.exe” file.
-
Move the executable to the target folder where it will execute.
rustc –explain E0308
Run executable
./hello
The response is “Hello, World!”.
Alternately, run by
cargo run
Keyboard alias
So that I can compile and run with a single command:
cr hello
I put this code to create a keyboard alias in my ~/.bash_profile:
alias cr="cargo run"
Explain Compile Errors
rustc --explain E0308
Cargo Package Manager of Crates
In the Rust development environment, all tools are installed to the ~/.cargo/bin directory, where the Rust toolchain is installed, including rustc, cargo, and rustup.
-
Install https://crates.io is the Rust language’s library registry (like npm and pypi). On Linux and macOS systems:
curl https://sh.rustup.rs -sSf | sh
Response:
... warning: rustup should not be installed alongside Rust. Please uninstall your existing Rust first.
-
Notice the most downloaded libraries are rand, syn.
-
Build
cargo build
Toolchain management with rustup (at https://github.com/rust-lang/rustup.rs) rustup manages builds in a consistent way on every platform that Rust supports, enabling installation of Rust from the beta and nightly release channels as well as support for additional cross-compilation targets.
[dependencies] rand = "^0"
- ^0 specifies upgrade automatically up to, but not including “1.0.0”.
- ~.0.7.1 with a tilde specifies minimal upgrade automatically up to, but not including “0.8”.
Benchmarking with crate Criterion
Criterion crate has a separate directory and thus separate files from tests.
To benchmark non-public methods, use feature flags and wrappers.
Tokio for concurrent runtime
- VIDEO: Rust - Live coding implementation of non-blocking async agents on top of Tokio” by Boy Maas
- VIDEO: Creating a Chat Server with async Rust and Tokio by Lily Mara, author of “Refactoring to Rust” from Manning Publications
github.com/tokio-rs/tokio for concurrent runtime.
musl for cross-compilation
Log4r for Logging
Language features
Strings
A string can be define two ways: &str or String:
fn mail() { let example_str: &stsr = "Howdy"; // immutable let example_string: String = String::from("Partner"); }
“characters (u8s)” gives the impression that individual characters themselves are 8-bit, when inside a string, they can be 8-bit, 16-bit, 24-bit, or 32-bit depending on the character. The “u8s” specifies bytes of memory encoding the string, not individual characters.
Borrow Checker
VIDEO: Niko Matsakis - Rust: Putting Ownership to Use by Curry On!
This keeps developers from spending hours or days hunting down data race conditions.
But this can be a daunting challenge to many new to Rustlang.
Closures
VIDEO: Closures - Rust by danlogs
A closure gives an inner function access to an outer function’s scope, even after the outer function has returned.
A closure is a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
In JavaScript, closures are created every time a function is created, at function creation time.
To use a closure, define a function inside another function and expose it. To expose a function, return it or pass it to another function.
Generics
Traits for inheritance
Traits were added in Rust 0.4 as a means to provide inheritance;
interfaces were unified with traits and removed as a separate feature.
For interfacing with C, Rust has a foreign function interface (FFI) that can be called from, e.g., C language, and can call C. While calling C++ has historically been problematic (from any language), Rust has a library, CXX, to allow calling to or from C++, and “CXX has zero or negligible overhead.”
The type system supports a mechanism similar to type classes, called traits, inspired by the Haskell language. This facility is for ad hoc polymorphism, achieved by adding constraints to type variable declarations.
Rust Result and Option types …
Option types
“Once you’ve used them, any language with null starts to feel like a loaded gun pointed at your foot.”
Browser javascript GUI
VIDEO: Rust in the Browser for JavaScripters: New Frontiers, New Possibilities by Coding Tech
GraphQL API
https://www.youtube.com/watch?v=QXJ0wKBLt-8 Rust and GraphQL: A match made in heaven
Speed with database
Rust at speed — building a fast concurrent database by Jon Gjengset
Video tutorials
Let’s Get Rusty:
- https://www.youtube.com/watch?v=m76sRj2VgGo”>The Box Smart Pointer in Rust</a>
VIDEO: Packt: “Ultimate Rust Crash Course” Oct. 2020 by Nathan Stocks.
VIDEO “Rust Crash Course | Rustlang” by Traversy Media
8.5 hour video course “The Rust Programming Language” by Dmitri Nesteruk (“semi-retired” Quant in the UK) shows use of IntelliJ IDEA.
Tutorials
BOOK: “The Rust Programming Language” at rust-lang.org/learn with code at https://github.com/rust-lang/rustlings/
https://doc.rust-lang.org/rust-by-example/index.html
[3] BOOK: Manning: “Rust in Action” Aug. 2021 [at OReilly.com] by Tim McNamara, with https://github.com/rust-in-action/code
At the Rust YouTube channel are recordings of meetings.
Bastian Gruber, author of VIDEO: manning.com/books/rust-web-development and https://rustwebdevelopment.com covers Logging, Error Handling, Vec, HashMap, String, Traits, https://git.sr.ht/~gruberb/onetutorial
“Rust, Wright’s Law, and the Future of Low-Latency Systems” at ScyllaDB’s P99 Conf. by Bryan Cantrill
https://www.youtube.com/watch?v=Uqi9xwlFbEc