Rustlings Topic: Tests

Tests are Rust functions that verify that the non-test code is functioning in the expected manner. The bodies of test functions typically perform these three actions:

  1. Set up any needed data or state.
  2. Run the code you want to test.
  3. Assert the results are what you expect.

Let’s look at the features Rust provides specifically for writing tests that take these actions, which include the test attribute, a few macros, and the should_panic attribute.

You may find solution code for the topic from my repo.

Rustlings Topic: Traits

A trait tells the Rust compiler about functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way. We can use trait bounds to specify that a generic type can be any type that has certain behavior.

Traits are similar to a feature often called interfaces in other languages, although with some differences.

trait is one of the most important Rust features that you must be familiar with. There is so many trait that Rust provides. Such as Clone, Copy, Debug, Default, Deref, From, Ord etc.

Sometimes you can use Derive macros macro, or sometimes you have to implement the trait by yourself. Unfortunately, rustling only focuses on the basic usage of the trait rather than introducing Rust providing traits. So if you want to know more about common traits, I suggest you read blog article.

You may find solution code for the topic from my repo.

Rustlings Topic: Option

Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. Option types are very common in Rust code, as they have a number of uses:

  • Initial values
  • Return values for functions that are not defined over their entire input range (partial functions)
  • Return value for otherwise reporting simple errors, where None is returned on error
  • Optional struct fields
  • Struct fields that can be loaned or “taken”
  • Optional function arguments
  • Nullable pointers
  • Swapping things out of difficult situations

You may find solution code for the topic from my repo.

Rustlings Topic: Generics

Generics is the topic of generalizing types and functionalities to broader cases. This is extremely useful for reducing code duplication in many ways, but can call for rather involving syntax. Namely, being generic requires taking great care to specify over which types a generic type is actually considered valid. The simplest and most common use of generics is for type parameters.

You may find solution code for the topic from my repo.

Rustlings Topic: Collections

Rust’s standard library includes a number of very useful data structures called collections. Most other data types represent one specific value, but collections can contain multiple values. Unlike the built-in array and tuple types, the data these collections point to is stored on the heap, which means the amount of data does not need to be known at compile-time and can grow or shrink as the program runs.

This exercise will get you familiar with two fundamental data structures that are used very often in Rust programs:

A vector allows you to store a variable number of values next to each other. A hash map allows you to associate a value with a particular key. You may also know this by the names unordered map in C++, dictionary in Python, or an associative array in other languages.

You may find solution code for the topic from my repo.

Rustlings Topic: Modules

Rust has a number of features that allow you to manage your code’s organization, including which details are exposed, which details are private, and what names are in each scope in your programs. These features, sometimes collectively referred to as the module system, include:

  • Packages: A Cargo feature that lets you build, test, and share crates
  • Crates: A tree of modules that produces a library or executable
  • Modules and use: Let you control the organization, scope, and privacy of paths
  • Paths: A way of naming an item, such as a struct, function, or module

You may find solution code for the topic from my repo.

Pagination