Code Room
Code reviewHardcr-g174
Subject Uninitialized memoryLevel Senior–Staff~35 minCommon in Code quality & review interviewsIndustries Software development

Question

Review this Rust function that pre-sizes a buffer before reading into it.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewrust
use std::io::Read; fn read_n(mut r: impl Read, n: usize) -> std::io::Result<Vec<u8>> {    let mut buf: Vec<u8> = Vec::with_capacity(n);    unsafe { buf.set_len(n); }    let got = r.read(&mut buf)?;    buf.truncate(got);    Ok(buf)}
Run or narrate your approach, then ask the coach.