Rust offers a multitude of ways to convert
a value of a given type into another type.
The simplest form of type conversion is a type cast expression. It is denoted with the binary operator as
.
For instance, println!("{}", 1 + 1.0);
would not compile, since 1
is an integer while 1.0
is a float. However, println!("{}", 1 as f32 + 1.0)
should compile. The exercise using_as
tries to cover this.
Rust also offers traits that facilitate type conversions upon implementation. These traits can be found under the convert
module. The traits are the following:
From
and Into
covered in from_into
TryFrom
and TryInto
covered in try_from_into
AsRef
and AsMut
covered in as_ref_mut
ld both compile and run without panicking. These should be the main ways within the standard library to convert data into your desired types.
You may find solution code for the topic from my repo.
The Clippy tool is a collection of lints to analyze your code so you can catch common mistakes and improve your Rust code.
If you used the installation script for Rustlings, Clippy should be already installed. If not you can install it manually via rustup component add clippy
.
You may find solution code for the topic from my repo.
Rust’s macro system is very powerful, but also kind of difficult to wrap your head around. We’re not going to teach you how to write your own fully-featured macros. Instead, we’ll show you how to use and create them.
You may find solution code for the topic from my repo.
In most current operating systems, an executed program’s code is run in a process, and the operating system manages multiple processes at once. Within your program, you can also have independent parts that run simultaneously. The features that run these independent parts are called threads.
You may find solution code for the topic from my repo.
This section will teach you about Box, Shared-State Concurrency and Iterators.
You may find solution code for the topic from my repo.
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:
- Set up any needed data or state.
- Run the code you want to test.
- 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.
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.
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.
I wanted to check how many claps I got for each post without entering the post. Code for minimal, non-clapable counts? had a good code snippet to start with. So I gave it a try.
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.