Michael Dacanay
5 min readApr 29, 2023

I know I probably should work on my iOS app for a course I’m taking, but I just feel like learning Rust at the moment.

Rust/Go course offered by Microsoft

I also have an ulterior motive, which is I want to be a Microsoft Learn Student Ambassador. Partly because of the $150 in monthly Azure credits and also because I am feeling really motivated to prove myself after not matching in the Google internship project search stage i.e. host matching. It is disappointing, especially after passing all the coding rounds but maybe it is good for me in the long run so I don’t become complacent.

I will start with an excerpt on the best way to learn Rust:

Rust requires a bit of theoretical knowledge before you can productively write Rust code. Make sure to follow along with this course or other Rust learning resources before you start your development. After you reach a basic understanding of the language, practice writing code as much as possible.

Here are the benefits:

Type safe: The compiler assures that no operation will be applied to a variable of a wrong type.

Memory safe: Rust pointers (known as references) always refer to valid memory.

Data race free: Rust’s borrow checker guarantees thread-safety by ensuring that multiple parts of a program can’t mutate the same value at the same time.

Zero-cost abstractions: Rust allows the use of high-level concepts, like iteration, interfaces, and functional programming, with minimal to no performance costs. The abstractions perform as well, as if you wrote the underlying code by hand.

Minimal runtime: Rust has a minimal and optional runtime. The language also has no garbage collector to manage memory efficiently. In this way, Rust is most similar to languages like C and C++.

Targets bare metal: Rust can target embedded and “bare metal” programming, making it suitable to write an operating system kernel or device drivers.

Rust is the memory-safe version of C/C++. It has all the performance advantages of C/C++, mainly having no garbage collector (to clean up variable memory that is no longer used). Which begs the question: why does rust not require a garbage collector?

Rust does not require a garbage collector because it uses a different approach to memory management that relies on ownership, borrowing, and lifetimes.

In Rust, each value has an owner, which is the variable that is responsible for allocating and freeing the memory used by the value. Ownership can be transferred between variables, but at any given time, there can only be one owner of a value. When the owner goes out of scope, Rust automatically deallocates the memory used by the value.

Borrowing is another key concept in Rust’s memory management model. Borrowing allows multiple references to a value without transferring ownership. The borrow checker, which is part of Rust’s compiler, enforces a set of rules to ensure that borrowing is done safely and without creating data races or memory safety issues.

Finally, Rust’s lifetime system tracks the lifetime of values and ensures that references to values are valid for the duration of their lifetime. The lifetime of a value is the period of time during which it is accessible and valid, and it is determined by the ownership and borrowing rules of Rust.

By combining ownership, borrowing, and lifetimes, Rust achieves memory safety and thread safety without the need for a garbage collector. This approach eliminates the performance overhead and unpredictability of garbage collection, making Rust a viable option for systems programming, where low-level control and performance are critical.

The Rust module system is similar to Python’s modules.

The system is composed of crates, modules, and paths, and tools to work with those items.

Crates: A Rust crate is a compilation unit. It’s the smallest piece of code the Rust compiler can run. The code in a crate is compiled together to create a binary executable or a library. In Rust, only crates are compiled as reusable units. A crate contains a hierarchy of Rust modules with an implicit, unnamed top-level module.

Modules: Rust modules help you organize your program by letting you manage the scope of the individual code items inside a crate. Related code items or items that are used together can be grouped into the same module. Recursive code definitions can span other modules.

Paths: In Rust, you can use paths to name items in your code. For example, a path can be a data definition like a vector, a code function, or even a module. The module feature also helps you control the privacy of your paths. You can specify the parts of your code that are accessible publicly versus parts that are private. This feature lets you hide the implementation details.

In healthcare, an extern is a “person working in but not living in an institution, such as a nonresident doctor or other worker in a hospital.”

In Rust, extern is a keyword that is used to indicate that the following block of code will contain declarations of external items that are provided by another crate or foreign language.

For example, if you want to use a function or variable that is defined in another Rust crate, you can declare it using extern crate followed by the name of the crate:

extern crate rand;

use rand::Rng;

fn main() {
let mut rng = rand::thread_rng();
let random_number = rng.gen_range(1..101);
println!("Random number: {}", random_number);
}

In this example, we are using the rand crate to generate a random number. We declare the crate using extern crate rand, which tells Rust to load the crate and make its contents available to our program.

From this example, it seems a crate is like a Python function. But it actually isn’t?

The Rust Standard Library std contains reusable code for fundamental definitions and operations in Rust programs. This library has definitions for core data types like String and Vec<T>, operations for Rust primitives, code for commonly used macro functions, support for input and output actions, and many other areas of functionality.

There are tens of thousands of libraries and third-party crates available to use in Rust programs most of which can be accessed through Rust’s third-party crate repository crates.io.

This is similar to Python’s pypi.

std — The Rust standard library

  • std::collections— Definitions for collection types, such as HashMap.
  • std::env — Functions for working with your environment.
  • std::fmt — Functionality to control output format.
  • std::fs — Functions for working with the file system.
  • std::io — Definitions and functionality for working with input/output.
  • std::path — Definitions and functions that support working with file system path data.

By default, the std library is available to all Rust crates. To access the reusable code in a crate or library, we implement the use keyword. With the use keyword, the code in the crate or library is "brought into scope" so you can access the definitions and functions in your program.

Michael Dacanay
Michael Dacanay

Written by Michael Dacanay

Intern at Tesla, Fidelity. Student at North Carolina State University

No responses yet