expression
Borrow a dependency for a single operation
Method or function injection is useful when a dependency is needed only during one call. The caller retains ownership, and no service lifetime or stored field is needed. The example uses two configured rule values, illustrating that runtime configuration does not automatically require dynamic dispatch. Expected result (NOT RUN): false true Common mistake: Storing a dependency permanently when only a temporary borrow is needed. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
fn check(rule: &impl Rule, value: u32) -> boolWorking Example
Example Snippetrust
trait Rule { fn allows(&self, value: u32) -> bool; }struct Maximum(u32);impl Rule for Maximum { fn allows(&self, value: u32) -> bool { value <= self.0 }}fn check(rule: &impl Rule, value: u32) -> bool { rule.allows(value) }fn main() { let small = Maximum(5); let large = Maximum(10); println!("{} {}", check(&small, 7), check(&large, 7));}