lifetime

Rust Select captured parameters with use bounds

A precise capture list can preserve a needed borrow while omitting an unrelated one. For the Rust 1.85 free-function baseline, include all in-scope type and const parameters, and list lifetimes first. Do not mix this form with anonymous argument-position impl Trait parameters. Expected output (not executed): checked 42 Why: The result carries the id relationship, not the unrelated note relationship. Common mistake: Omitting T from the capture list, or treating a capture list as an outlives bound.

Formal Syntax & Structure

rust syntaxrust
fn view<'a, 'b, T>(value: &'a T, note: &'b str) -> impl std::fmt::Debug + use<'a, T>

Working Example

Example Snippetrust
fn view<'a, 'b, T: std::fmt::Debug>(value: &'a T, note: &'b str)    -> impl std::fmt::Debug + use<'a, T>{    println!("{note}");    value}fn main() {    let id = 42;    let result;    {        let note = String::from("checked");        result = view(&id, &note);    }    println!("{result:?}");}
Rust Reference