Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github

LinkedIn

Efficient, secure, performant concurrent systems programming that compiles to machine running byte-code

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

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:

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:

Social ecosystem

Rust developers are called “Rustlings” or “Rustaceans”.

#RustLang on Twitter.

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

  1. 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

  2. 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)
    
  3. 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

  1. Obtain the folder:

    git clone https://github.com/wilsonmar/rustlang-samples
    cd rustlang-samples
    
  2. 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:

VisualRust IDE?


Sample programs

The most thorough

https://github.com/TheAlgorithms/Rust

Hello World

  1. 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.

  2. 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.

  3. 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.

  4. 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.
    
  5. Notice the most downloaded libraries are rand, syn.

  6. 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

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

[2] “The Rustonomicon”

[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

Rust Programming Techniques