Introduction to Rust
It is better to rust out than wear out. -- Edwin Markham
Files for this tutorial are at: keypuncher/Rust/Intro/hello_world.
I have heard a lot about Rust recently at work so I have decided to try it out to see what the fuss is all about. According to Wikipedia Rust is a programming language created by Graydon Hoare who states that the name comes from this difficult to eradicate fungus: Rust (fungus).
Graydon Hoare created Rust as a personal project while working at Mozilla Research in 2006. Mozilla officially sponsored the project in 2009. In the years following the first stable release in May 2015, Rust was adopted by companies including Amazon, Discord, Dropbox, Google (Alphabet), Meta, and Microsoft. In December 2022, it became the first language other than C and assembly to be supported in the development of the Linux kernel.
Rust has a great package manger called Cargo https://doc.rust-lang.org/cargo/. This means unlike with languages such as C or C++ there is a centralized means to distribute versions of Rust. According to https://doc.rust-lang.org/cargo/guide/why-cargo-exists.html:
[Cargo] is a tool that allows Rust packages to declare their various dependencies and ensure that you’ll always get a repeatable build.
In this tutorial I am using Cargo version 1.82.0
Downloading and Installing
I’m pretty much following along with https://doc.rust-lang.org/cargo/getting-started/installation.html but I will note if there is anywhere I diverge from the instructions given in that link.
Windows 11
Download the windows version of Rust by downloading rustup-init.exe from the link above. Open the installer file and enter 1 to do the quick install via Visual Studio Community. Proceed through the installer steps, (note that the Win11 SDK took some time for me to download so be patient). Also note that you keep the rustup-init.exe window open to press 1 again to install for msvc. When it’s finished close the rustup_init.exe window. See https://visualstudio.microsoft.com/vs/community/ for more on Visual Studio Community.
Now open Powershell and create a directory called cargo_test
mkdir cargo_test
cd cargo_test
cargo new hello_world
The directory structure Cargo creates is covered here: https://doc.rust-lang.org/cargo/getting-started/first-steps.html, view it in PowerShell using the tree command:
tree .
For the moment I’m most interested in what’s in src/main.rs:
fn main() {
println!("Hello, world!");
}
Cargo generated a “hello world” Rust program for us! Let’s build it:
cd hello_world
cargo build
Then run it with
.\target\debug\hello_world.exe
We can also build and run in the same step with:
cargo run
We can do the same thing for a release build:
cargo run --release
The difference between debug and release mode is given here: https://doc.rust-lang.org/cargo/guide/creating-a-new-project.html
Compiling in debug mode is the default for development. Compilation time is shorter since the compiler doesn’t do optimizations, but the code will run slower. Release mode takes longer to compile, but the code will run faster.
Ubuntu 22.04
https://www.rust-lang.org/tools/install
Prereqs:
sudo apt install curl
Then download Rust with the following command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Then push 1 for the default installation.
After the installation is complete close and re-open the terminal.
Ensure that .cargo/bin is somewhere on your path:
echo $PATH
My cargo lives in /home/vagrant/.cargo/bin and my version of rustc is 1.83.0.
The only difference from the Windows instructions is you omit the .exe after the build step:
target/debug/hello_world
Adding Dependencies
See: https://doc.rust-lang.org/cargo/guide/dependencies.html
Add the following to your Cargo.toml file:
[dependencies]
time = "0.1.12"
regex = "0.1.41"
My complete Cargo.toml file now looks like this:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
[dependencies]
time = "0.1.12"
regex = "0.1.41"
Now re-run cargo build:
cargo build
This will add a lot of files to your Cargo.lock file. This file is a bit long but it is available on my github at keypuncher/Rust/Intro/hello_world/Cargo.lock.
Note that if either dependency gets updated, it should build with the same revision until we choose to cargo update.
When we are through Cargo can clean up for us:
cargo clean
This cleans up all cargo intermediary files, including dependencies. It does not clean source, Cargo.toml, nor Cargo.lock.
U.S. NSA Recommends a Migration to Memory-Safety
The U.S. National Security Agency (NSA) has recommended memory safe languages like Rust. Other memory safe languages mentioned in the document include Go, Java, Python, C#, and Swift.
What happens to C/C++ given this push for memory safety? It’s already being considered for C++: Can C++ Be Saved? Bjarne Stroustrup on Ensuring Memory Safety. How you might make your C++ memory safe is at Google Online Security Blog: Retrofitting spatial safety to hundreds of millions of lines of C++.
Doesn’t look promising for C, see Embedded - Memory safety in C:
Memory safety in C can be summed up in a few words: there isn’t any!
Enjoy!
I hope you enjoyed this very short Rust introduction as much as I loved writing it. Happy Rusting!
Feedback
As always, do make a comment or write me an email if you have something to say about this post!
Credits
https://learning.oreilly.com/library/view/programming-rust-2nd/9781492052586/
Programming Rust: by Jim Blandy, Jason Orendorff, and Leonora F. S. Tindall.
Rust in VS Code Rust with Visual Studio Code.