expression
Inject a dependency through a service constructor
Constructor injection makes a required dependency explicit and gives the service ownership of it. A generic service permits compile-time substitution without global mutable state. In this example the sink retains events in memory; no external messages are sent. Passing a real adapter at the application entry point would keep wiring outside domain logic. Expected result (NOT RUN): ["completed"] Common mistake: Constructing a hidden concrete dependency inside every business method, or introducing global state to share it. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
struct Service<S: Sink> { sink: S }Working Example
Example Snippetrust
trait Sink { fn record(&mut self, event: &str); }#[derive(Default)]struct MemorySink(Vec<String>);impl Sink for MemorySink { fn record(&mut self, event: &str) { self.0.push(event.to_owned()); }}struct Service<S> { sink: S }impl<S: Sink> Service<S> { fn new(sink: S) -> Self { Self { sink } } fn complete(&mut self) { self.sink.record("completed"); } fn into_sink(self) -> S { self.sink }}fn main() { let mut service = Service::new(MemorySink::default()); service.complete(); println!("{:?}", service.into_sink().0);}