The Rustlings exercises — part 1

To continue building my understanding of Rust, I searched for some simple Rust exercises. Hence, I dedicated my weekly personal work time to the Rustling exercises.

Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!

https://github.com/rust-lang/rustlings

I believe that this workshop is pretty neat. Thanks to everybody who contributed to it! I’ll split my notes into two posts, as Rustlings contain many (many!) exercises. Besides that, I need to learn about the more advanced themes such as threading. In those two posts, I’ll only describe the exciting bits. If you’re interested, you can find the solutions themselves on GitHub. I’d urge you to try by yourself, though.

Conditionals

if a > b {
a
}
b

Instead, you have to be explicit about this:

if a > b {
a
} else {
b
}

Move semantics

let vec0 = Vec::new();let mut vec1 = fill_vec(vec0);          // 1
  1. The vec1 variable now owns the vec0 parameter!

The idea is to pass a reference to vec0 instead so that that fill_vec() only borrows it.

fn fill_vec(vec: &[i32]) -> Vec<i32> { // 1
let mut vec = vec.to_vec(); // 2
  1. Pass a slice
  2. Create a new Vec from the slice and return it

more_semantics3.rs is much easier as the compiler outputs the correct hint to fix the issue.

Structures

  1. Use &str instead of String.
  2. When using references, we need to take care of the lifetime.

struct2.rs teaches about copying existing structures into new structures. Rust allows you to create a new struct from an existing one by defining only different field values and copying the same ones using ... You can read more about it here.

Collections

fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for i in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2.
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
v
}

The syntax already uses mutability, i.e., mut and iter_mut(), so that is not an issue. But iter_mut(), as well as iter(), return references. Hence, we have to dereference both the left and the right side of the assignment. Regarding HashMap, it seems there's no available macro like vec![] for Vec.

Error handling

In errors.rs, I got confused: I tried to create a custom Err type. But one only needs to replace Some with Ok, and use the standard Result type.

I solved the error2.rs by using match. I forgot that each match clause must end with a comma.

match qty {
Ok(i) => Ok(i * cost_per_item + processing_fee),
Err(e) => Err(e),
}

It’s not the way. It’s much easier to use map() and the proper closure. There's no comma between the closure's parameter(s) and its body as opposed to match.

To me, the compiler hint was no help to solve error_handlingn.rs. I made the wrong assumption initially and tried to use CreationError as the return type. It didn't help that I didn't read the book's section about the Box type.

Generics

Options

Results

Tests

Iterators

In iterator2.rs, I learned that a char.to_uppercase() doesn't return a String but a dedicated ToUpperCase type. The reason is that some languages don't have a simple mapping from lower case to upper case, e.g., German ß.

TIL: join("") function when you need to collect additional items in between. The associated type is std::slice::Join. I didn't find the association between Vec<> and join()...

I started iterator3.rs with list_of_results() because it's (much) easier. division_results is of type Map, fn(i32) -> ?>. x must be of type >>. It means we only need to collect() the Iterator: it will trigger the map() closure. Done.

result_with_list() requires a <Result<Vec<i32>, DivisionError>>. I had to go through the documentation to find the collect() function that applies explicitly to an iterator of Result. The idea is to collect first like in the previous function, then make it back to an iterator of Result, and finally collect() again. As mentioned in the documentation, you need to help the compiler with collect() because, in general, it cannot infer types correctly:

Because collect() is so general, it can cause problems with type inference. As such, collect() is one of the few times you'll see the syntax affectionately known as the 'turbofish': ::<>. This helps the inference algorithm understand specifically which collection you're trying to collect into.

— pub fn collect<B>(self) -> B

Solving iterators4.rs is easy with recursion. The compiler hints about ranges: it's actually relatively easy combining them and the fold() function. The latter is fairly common in Functional Programming. This is it. In the next post, I'll provide the notes I took while solving the remaining exercises.

The complete source code for this post can be found on Github.

To go further:

Originally published at A Java Geek on June 13th 2021

Dev Advocate @Hazelcast . Former developer and architect. Still teaching, learning and blogging.