TIL: Rust & Advent of Code
For the first time I’ve decided to try to rewrite some solutions to Rust.
Data structres
- if I have a counter, it has to be
mut, because it’s mutable:let mut sum = 0; - when assigning a value which cannot be
Nonethen we useSometo avoid rull referece errorslet mut first = None; if first.is_none() { first = Some(ch); }
Iterating
- Rust uses
if letconstruction for pattern matching and conditional execution:- firstly it checks the condition
- then it extraacts values from
Option - then it binds them to variables
if let (Some(first_number), Some(last_number)) = (first, last) {
...
}
-> is equivalent to Python:
if first is not None and last is not None:
first_number = first
last_number = last
else:
...
Casting
TBA
Reading from a file
TBA