'Why use nested blocks?

I just came across this piece of code in base64 crate:

 buffer.resize(decoded_len_estimate, 0);

let bytes_written;
{
    let buffer_slice = &mut buffer.as_mut_slice()[starting_output_len..];
    bytes_written = decode_helper(input_bytes, num_chunks, config, buffer_slice)?;
}

buffer.truncate(starting_output_len + bytes_written);

See https://github.com/marshallpierce/rust-base64/blob/b63975f0a3d2e724c611bf6cd7213ae6bcb065a3/src/decode.rs#L165-L169

What is the reason for using this style of declaring the variable bytes_written and then use this nested block. What problem does this solve? Why not just use this code:

 buffer.resize(decoded_len_estimate, 0);

let buffer_slice = &mut buffer.as_mut_slice()[starting_output_len..];
let bytes_written = decode_helper(input_bytes, num_chunks, config, buffer_slice)?  
buffer.truncate(starting_output_len + bytes_written);

Can someone help me with that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source