expression
Require a builder field before exposing build
Typestate encodes a stage in a type parameter. A build method implemented only for Builder<Ready> is unavailable before the required input is supplied. This example uses the state itself to hold the required value, so there is no Option to unwrap at construction. Keep stage construction controlled in a module in real APIs. Expected result (NOT RUN): report Common mistake: Adding build to every generic state or assuming a required field is automatically semantically valid. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
Builder<Missing> -> Builder<Ready>Working Example
Example Snippetrust
struct Missing;struct Ready(String);struct Builder<S> { state: S }impl Builder<Missing> { fn new() -> Self { Self { state: Missing } } fn label(self, label: String) -> Builder<Ready> { Builder { state: Ready(label) } }}impl Builder<Ready> { fn build(self) -> String { self.state.0 }}fn main() { let label = Builder::new().label("report".into()).build(); println!("{label}");}