udoprog.github.io

Advent of Rust Day 1 - Everything has a scope

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

I decided the use the following compact expression to open and read the entire contents of a file:

let mut data = String::new();
File::open(path)?.read_to_string(&mut data)?;

Desugared, it would read as something like this:

let mut data = String::new();

{
    let mut _f = File::open(path)?;
    _f.read_to_string(&mut data)?;
}

This means that the file is closed immediately after its contents has been read, and is not accessible anywhere else in the scope.