Skip to content

Learning Rust - Final

Posted on:April 28, 2023 at 03:22 PM

OOP in Rust

Rust is not designed as an object oriented programming language but it does have capabilities that allow you to use the design practices of it. For example, structs are made as object and the impl block creates methods for them.

Encapsulation in Rust is default since the way to use a method or even a struct from a library is through the pub keyword, otherwise they remain unreachable by the main code trying to use it. Adding pub to a struct makes it public but not its fields.

Another good thing I learned was that when using a variable, some types have a method called as_ref() that instead of getting the value itself you can get a reference to it. An example of this is:

pub fn content(&self) -> &str {
    self.state.as_ref().unwrap().content(self)
}

Patterns

Rust is full of patterns that can be match anywhere, for that, the keyword match is the best option to work with, it’s basically a switch statement but in steroids. It’s an exahustive matching so you need to express what should happen for every posibility of the match, specially when using enums.

The while let keywords are useful for when we are tying to pop all the elements of a vector for example. Because it runs until a condition is no longer true such as checking for Some() value until you recieve a None. EX:

let mut optional = Some(0);

while let Some(i) = optional {
    if i > 9 {
        println!("Greater than 9, quit!");
        optional = None;
    } else {
        println!("`i` is `{:?}`. Try again.", i);
        optional = Some(i + 1);
    }
}

You can also use | for an or operator when matching a value and ..= is to match on a inclusive range of values. It can also be used with characters:

let x = 5;

match x {
    1..=5 => println!("one through five"),
    _ => println!("something else"),
}

A match guard is an additional if condition, specified after the pattern in a match arm, that must also match for that arm to be chosen. Match guards are useful for expressing more complex ideas than a pattern alone allows. When match guard expressions are on a match pattern, the Rust compiler will not try to check for exhaustiveness.

Unsafe Rust

Here I learned how to get away with basically cheating the compiler. It makes sense to do it sometimes since the compiler does not know your specific use case. To create some unsafe code you have to be on an unsafe block which is just instanciated by writing unsafe before any scope of code. The list of things we can do while on an unsafe block are:

Webserver

The last topic of the book covers how to create a multi-threaded webserver with a walkthrough of it. I mostly learned about raw http requests and the TCP protocol. Also learned that you can preallocate a Vec by creating it with Vec.with_capacity(). Which will probably be faster when doing maps and large computations on it.

Conclusion

Obviously, no amount of studying will make me “know” Rust, but I think this book was great and it definitely opened up to me a whole lot of programming knowledge I didn’t know or was rusty at. I will now start to create things with Rust, starting with Moti. I will also go throught The Primeagen’s course on Data Structures and Algorithms, that I will not do a devlog about but it’s an important part of some stuffs I want to do in the future. I loved the book and can’t recommend it enough!