expression
Choose a closed set of implementations with a factory
A factory centralizes construction decisions. When the set of options is closed and small, an enum can represent the result without heap allocation or dynamic dispatch. This factory accepts a configuration label and rejects unknown labels before returning a formatter. Expected result (NOT RUN): [ready] invalid: true Common mistake: Adding dynamic dispatch for a fixed choice when an enum is simpler. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
fn create(kind: &str) -> Result<Formatter, &'static str>Working Example
Example Snippetrust
enum Formatter { Plain, Bracketed }fn create(kind: &str) -> Result<Formatter, &'static str> { match kind { "plain" => Ok(Formatter::Plain), "bracket" => Ok(Formatter::Bracketed), _ => Err("unknown format") }}impl Formatter { fn render(&self, text: &str) -> String { match self { Self::Plain => text.into(), Self::Bracketed => format!("[{text}]") } }}fn main() { println!("{}", create("bracket").unwrap().render("ready")); println!("invalid: {}", create("other").is_err());}