70 %

Toast is in Beta!Learn more →

Chris Biscardi

Rust block scoping vs JavaScript do expressions

Sometimes I have a complex series of expressions that I need to perform to initialize a variable.

JS
let myPath = getSomePath();
myPath.pop();
myPath.push(".tmp");

In JS we can use the do expressions proposal, which looks like this:

JS
const x = do {
let myPath = getSomePath();
myPath.pop();
myPath.push(".tmp");
myPath
};

In Rust we don't need special syntax support because Rust has block expressions built-in. In this case we clone a PathBuf, pop the last segment off, push a new ".tmp" segment on, and then return the dir value.

rust
let tmp_dir = {
let mut dir = project_root_dir.clone();
dir.pop();
dir.push(".tmp");
dir
};

Note that in both the do expression and the block expression cases we isolate the complex expressions into another scope. This allows us to declare variables and generally isolate initialization logic from the rest of the program and make it more clear that we aren't using those temporary variables in the rest of the program. In JS this allows us to use const and in Rust it allows us to remove the mutability requirement on our variable.