std-method
BinaryHeap::try_reserve
Request room for at least len plus additional elements and receive an error if capacity cannot be reserved. Actual available capacity can be larger. Expected stdout (not executed): true Common mistake: Treating additional as an absolute capacity.
Formal Syntax & Structure
rust syntaxrust
fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>Working Example
Example Snippetrust
fn main() { use std::collections::BinaryHeap; let mut q = BinaryHeap::from([3, 4]); match q.try_reserve(8) { Ok(()) => println!("{}", q.capacity() >= q.len() + 8), Err(_) => println!("reservation failed"), }}