std-method

Vec::try_reserve

Request space for additional elements and report capacity overflow or allocation failure as an error. On error the existing contents are preserved. A deterministic oversized request for a nonzero-sized element type exercises overflow without requiring a huge allocation. Expected stdout (not executed): true [7] Common mistake: Assuming successful reservation changes length or guarantees future unrelated allocations.

Formal Syntax & Structure

rust syntaxrust
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

Working Example

Example Snippetrust
fn main() {    let mut values = vec![7_u8];    let result = values.try_reserve(usize::MAX);    println!("{} {values:?}", result.is_err());}
Rust Reference