-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
The traditional State pattern allows an object to alter its behavior when its internal state changes, appearing to change its "class" at runtime. By encapsulating state-specific behaviors into separate objects and delegating execution to the current state, we eliminate complex conditional logic and make state transitions explicit and manageable. Rust does not have classes, but the State pattern lets us evolve the behavior of a type.
As our calculator matures, it needs to behave differently in different contexts. In standard mode, it performs basic arithmetic operations. In scientific mode, it handles trigonometric functions, logarithms, and higher mathematics. In programmer mode, it deals with binary, octal, and hexadecimal numbers, plus bitwise operations. (We will implement Standard and Scientific modes in this chapter; Programmer mode follows the same pattern and its implementation is...