Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

with: Working with Closures

with as in “do X, but with this specific way of computing things.”

#![allow(unused)]
fn main() {
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

impl<T> Vec<T> {
    // Simplified. If the resize is larger than the current vec size, use the
    // closure to populate elements.
    pub fn resize_with(&mut self, new_len: usize, f: impl FnMut() -> T);
}

mod iter {
    // Create an infinite, lazy iterator using a closure.
    pub fn repeat_with<A, F: FnMut() -> A>(repeater: F) -> RepeatWith<F>;
}
}
  • with can appear as a suffix to communicate there is a specific function or closure that can be used instead of a “sensible default” for a computation.

    Similar to by.