expression
Define a trait around the consumer’s needs
A service port is a small interface expressing the capability a consumer needs. Dependency inversion means the business logic depends on this contract, rather than a particular storage implementation. Here a quote service needs only a price lookup; it does not depend on database connection or query APIs. The in-memory implementation is a teaching adapter, not a database. Expected result (NOT RUN): Some(12) None Common mistake: Exposing database-specific types in a business contract, or treating every failure as a missing product. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
trait Catalog { fn price(&self, id: u32) -> Option<u32>; }Working Example
Example Snippetrust
mod domain { pub trait Catalog { fn price(&self, id: u32) -> Option<u32>; } pub fn quote(catalog: &impl Catalog, id: u32, count: u32) -> Option<u32> { catalog.price(id)?.checked_mul(count) }}struct MemoryCatalog;impl domain::Catalog for MemoryCatalog { fn price(&self, id: u32) -> Option<u32> { (id == 1).then_some(4) }}fn main() { println!("{:?}", domain::quote(&MemoryCatalog, 1, 3)); println!("{:?}", domain::quote(&MemoryCatalog, 2, 3));}