Rust Tutorials

Rust provides a structured approach to learning systems programming and memory safety. Our tutorials cover Rust syntax, ownership concepts, and concurrent programming, suitable for beginners and experienced programmers. Through hands-on labs and practical examples, you'll gain proficiency in writing safe and efficient Rust code. Our Rust playground allows you to experiment with Rust features in real-time.

Rust Panic Handling and Memory Safety

Rust Panic Handling and Memory Safety

In this lab, we learn about the panic! macro in Rust, which can be used to generate a panic and start unwinding its stack, causing the program to report the panic message and exit. The runtime takes care of freeing all the resources owned by the thread by calling the destructor of its objects. We also look at an example of using the panic! macro to handle division by zero and verify that it doesn't result in memory leaks using Valgrind.
Rust
Box, Stack and Heap

Box, Stack and Heap

In this lab, the concept of boxing, stack allocation, and heap allocation in Rust is explored. All values in Rust are stack allocated by default, but they can be boxed (allocated on the heap) using the Box<T> type. A box is a smart pointer to a heap-allocated value, and when it goes out of scope, its destructor is called and the memory on the heap is freed. Boxing allows for the creation of double indirection and can be dereferenced using the * operator. The lab provides code examples and explanations of how boxing works and how it affects memory allocation on the stack.
Rust
Rust Operator Simplifies Error Handling

Rust Operator Simplifies Error Handling

In this lab, the ? operator is introduced as a way to make code cleaner when chaining results. It is used at the end of an expression returning a Result and simplifies the code by automatically handling the Err and Ok branches. The example provided demonstrates how to use the ? operator in Rust to handle various math operations and their potential errors.
Rust
Rust RAII Resource Management

Rust RAII Resource Management

In this lab, we will explore the concept of RAII in Rust, which enforces resource acquisition is initialization. This means that when objects go out of scope, their destructors are called and their owned resources are freed, eliminating the need for manual memory management and ensuring protection against resource leak bugs. We will also learn about the Drop trait in Rust, which allows custom destructor logic to be implemented for types that require it.
Rust
Rust Aliasing Concept Exploration

Rust Aliasing Concept Exploration

In this lab, the concept of aliasing is explored in the context of Rust programming language. Aliasing refers to the situation where multiple references are created to the same data, either as immutable or mutable borrows.
Rust
Mutable Borrowing in Rust Book

Mutable Borrowing in Rust Book

In this lab, we explore mutability in Rust by examining the concept of borrowing data through mutable and immutable references using the example of a book struct with functions that demonstrate the behavior of mutable and immutable borrowing.
Rust
Rust Borrowing Ownership Fundamentals

Rust Borrowing Ownership Fundamentals

In this lab, we learn about borrowing in Rust, which allows accessing data without taking ownership by using references ('&T') instead of passing objects by value ('T'). The borrow checker ensures that references always point to valid objects and prevents destruction of objects that are being borrowed.
Rust
Partial Move Destructuring in Rust

Partial Move Destructuring in Rust

In this lab, we learn about partial moves within the destructuring of a single variable, where both by-move and by-reference pattern bindings can be used simultaneously. This results in a partial move of the variable, allowing some parts to be moved while others can still be referenced. If a parent variable is partially moved, it cannot be used as a whole afterwards, but the parts that are only referenced and not moved can still be used.
Rust
Rust Mutability Ownership Demonstration

Rust Mutability Ownership Demonstration

In this lab, the mutability of data is demonstrated through Rust programming language by showcasing how ownership and mutability can be changed when a box is moved from an immutable state to a mutable state.
Rust
Ownership and Moves

Ownership and Moves

In this lab, it is explained that in Rust, variables have ownership of resources and can only have one owner, which prevents resources from being freed multiple times. When variables are assigned or function arguments are passed by value, the ownership of resources is transferred, known as a move. After the move, the previous owner can no longer be used to avoid creating dangling pointers. The code example demonstrates these concepts by showing how the ownership of stack-allocated and heap-allocated variables is transferred and how accessing a variable after its ownership has been moved leads to errors.
Rust
Rust Comment Types and Documentation

Rust Comment Types and Documentation

In this lab, we learn about the different types of comments supported by Rust, including regular comments and doc comments. Regular comments can be line comments, which start with two slashes and go to the end of the line, or block comments, which are enclosed between / and / and can be nested. Doc comments, on the other hand, are used to generate HTML library documentation and start with /// or //!.
Rust
Exploring Rust Tuples and Transposing Matrices

Exploring Rust Tuples and Transposing Matrices

In this lab, we explore tuples in Rust. Tuples are collections of values of different types and are constructed using parentheses. They can be used as function arguments and return values, allowing functions to return multiple values. Tuples can also be used as members of other tuples. Rust provides tuple indexing to access values in a tuple. Tuples are printable and can be destructured to create bindings. Additionally, we learn how to add the fmt::Display trait to a struct to customize its printing format. Finally, we are given an activity to implement a transpose function that swaps two elements in a matrix.
Rust
Literals and Operators

Literals and Operators

In this lab, we explore the use of literals and operators in Rust. Literals such as integers, floats, characters, strings, booleans, and the unit type can be expressed using their literal forms. Integers can also be expressed in hexadecimal, octal, or binary notation. Underscores can be used to improve readability in numeric literals. Rust supports scientific E-notation for float literals. Operators such as addition, subtraction, logical operations, bitwise operations, and shift operations are available in Rust.
Rust
Rust Primitive Types Exploration

Rust Primitive Types Exploration

In this lab, you will learn about the various primitives provided by Rust, including scalar types such as signed and unsigned integers, floating-point numbers, Unicode scalar values, boolean values, and the unit type. You will also explore compound types like arrays and tuples, and learn how to annotate variables with their respective types or allow Rust to infer the types from context.
Rust
Rust Formatting and Display Trait

Rust Formatting and Display Trait

In this lab, we learned about formatting in Rust and how to use the format! macro to format variables. We saw that formatting is specified using a format string, and different argument types can be used to format the same variable in different ways. The most common formatting trait is Display, which handles cases where the argument type is left unspecified. We saw an example of implementing the Display trait for a City struct, where we formatted the latitude and longitude values. We also saw an example of a Color struct and were tasked with implementing the Display trait for it to display the RGB values and their hexadecimal representation.
Rust
Implement fmt::Display for List in Rust

Implement fmt::Display for List in Rust

In this lab, we are implementing fmt::Display for a structure called List which contains a Vec in Rust. The challenge is to handle each element sequentially using the write! macro, as it generates a fmt::Result which needs to be properly handled. To address this, we can use the ? operator to check if write! returns an error and return it if it does, otherwise continue with the execution. By implementing fmt::Display for List, we can iterate over the elements in the vector and print them within square brackets, separated by commas. The task is to modify the program to also print the index of each element in the vector. The expected output after the modification is [0: 1, 1: 2, 2: 3].
Rust
Customizing Rust Struct Output with fmt::Display

Customizing Rust Struct Output with fmt::Display

In this lab, you will learn how to implement the fmt::Display trait in Rust to customize the output appearance of a structure. You will also explore the difference between fmt::Display and fmt::Debug, as well as the limitations of fmt::Display for generic container types. Finally, you will have an activity to implement the fmt::Display trait for a new Complex struct and print it in a specific format.
Rust
Printable Types in Rust's Standard Library

Printable Types in Rust's Standard Library

In this lab, it is explained that in order to use the std::fmt formatting traits, types must have an implementation to be printable, which can be automatically provided for types in the std library.
Rust
  • Prev
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • ...
  • 14
  • Next