rustlings 3
Published at 2024/08/10
Snapshotting Context
I think it will help my brain to get in flow to have a good area of context when I open a project.
In Rustlings, I decided to open the folder containing my rustlings exercise instead of just opening the VSCode editor with the exercise file. This loaded a piece of context I was forgetting: the README.md
file in each exercises/
folder. I opened the folder with $ code exercises/options
and found out what I’d been missing. I’ve been going about this all wrong! Or just a little wrong, anyhow. I intend to loop back around once I finish Rustlings for the first time, and I expect the review will give me lots of value in treading the paths in my mind - creating those new rusty neurons through repetition.
Another piece, if I might digress, of snapshottable context has been polluting my browser for a while. When I’m working on a topic that requires a bunch of research - I often end up opening new tabs that are linked from my current reading. This results in a window of multiple (currently… 60) open tabs of various sorts that I am afraid to lose. It would be hard to find these tabs again, it took me some work to get here! In this way the tabs are becoming a type of persistent storage for my brain. The problem is that they suck up a bunch of RAM, temporary resources that my computer needs to function well. Another problem is that sometimes I never return to the tabs; then they are wasted space. Sometimes I close a tab and bookmark it; but I almost never return to that bookmark. It’s just a way to soothe the hurt inherent in the idea of losing the tab forever. Today I came across an idea for a better way to manage these links, that uses the tools I already have.
Did you know there is a way to bookmark all the tabs in a window? Furthermore, through the bookmarks manager, I can reopen all the tabs in a folder. Therefore! I can make a new folder for bookmarks that can hold my tabs related to a PROJECT. Then, I can drop all the window’s tabs into that folder, close them up, and open it when it’s time to think about that project again. Maybe this will help? I’ll write here if it does. End digression.
Options in Rust
I enjoy that Rust has the mathematical-feeling idea that if we are matching enums, we need to provide facilities that cover all cases. Option
s, Some
, and None
bring this idea full circle for me.
Options1
I think that my code is wrong, looking at the tests. My code will output None in some cases where Some(0) is the correct response… there’s no ice cream after 10pm (22)
if (time_of_day < 22) {
return Some(5)
} else {
return None
}
However, I want to see what the error output looks like, so I’m running it without this. First though I have to fix some compiler errors - there was an issue I was supposed to fix in the tests. There’s a comparison between 5 and ‘icecreams’ an Option; I need to extract the value from the Option. I think the necessary sugar comes from https://doc.rust-lang.org/std/option/
let icecreams = maybe_icecream(12);
assert_eq!(icecreams?, 5);
Nope!! Actually the ? is for returning None or Some(T), still options.
let icecreams = maybe_icecream(12);
assert_eq!(icecreams.expect("there's icecream before 10pm"), 5);
Alrighty!! Now we have arrived at compilation. I don’t know if this was the right way to pull a value out of a Some, but it is one way. However… our tests are failing for the above reason.
failures:
tests::check_icecream stdout
thread 'tests::check_icecream' panicked at exercises/options/options1.rs:31:9:
assertion `left == right` failed
left: None
right: Some(0)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::check_icecream
So then. Correcting the issue we noted earlier - some of the cases (22<t<24)
there is a reason to return Some(0) instead of None.
Once I change that piece of logic, I pass the tests, so I AM DONE! With Options1.
Options2
This one ended up being a lot faster for me.
if let
and while let
end up with some similarities to the assignment expression (a.k.a. walrus operator) :=
in Python. A difference is that these can act like a one-case match expression, and so can be used to extract values inside even nested options. Neat!
Options3
And now knowing about ref
I can borrow amidst a match statement, instead of having the match be all-consuming. Also neat!