lifetime
Rust Exclude unnecessary lifetime capture with use<>
A precise capture list can exclude an input lifetime when the hidden return type does not need it. For a count computed from a borrowed label, the result can then be used after the label has been dropped. The use bound is available for this free-function pattern from Rust 1.82. Expected output (not executed): 3 Why: The opaque type does not capture the input lifetime. Common mistake: Adding use<> to a function whose hidden type really borrows the input.
Formal Syntax & Structure
rust syntaxrust
fn count(s: &str) -> impl std::fmt::Display + use<>Working Example
Example Snippetrust
fn count(text: &str) -> impl std::fmt::Display + use<> { text.len() }fn main() { let n; { let label = String::from("box"); n = count(&label); } println!("{n}");}