Advent of Rust Day 5 - Have your bounds and eat them too!
This is a series where I’ll be discussing interesting Rust tidbits that I encounter when solving Advent of Code 2017.
You can find the complete (spoiler) solution here: udoprog/rust-advent-of-code-2017
Attempting to access data that is out of bounds in a collections could potentially ruin your day.
To combat this, rust provides ‘safe’ alternatives for slice indexing in the form of get
and
get_mut
.
Note: these are available on Vec
because it implements Deref<Target = [T]>
, which causes rust
to look for methods there as well.
These return an Option<&T>
and Option<&mut T>
, requiring you to check that the provided index
was present, before attempting to deal with the data.
So this:
let mut v = vec![1, 2, 2];
println!("data = {}", v[2]);
Becomes this:
let mut v = vec![1, 2, 2];
match v.get(2) {
Some(value) => println!("data = {}", value),
None => println!("no value present :("),
}