liveBook · Manning

Product type

As explained in Write Powerful Rust Macros

A product type is a composite type whose possible values are combinations of values from all its component fields.

A product type combines fields, so each possible value contains one value from every component. Structs are the familiar example. The number of possible states equals the product of the fields’ possible values, which can create many combinations to validate. section 7.3

Counting struct states

A struct containing a u8 and a boolean has 256 × 2 = 512 possible states. A value containing 120 and true differs from one containing 119 and true. That flexibility can require conditionals to determine whether a state meets the application’s requirements. section 7.3

Product types belong to algebraic data types and contrast with sum types. Sum types restrict a value to one alternative, making their smaller state space well suited to pattern matching; Rust enums provide this model. section 7.3

Figure 7.2 The myriad of options of a product type versus the simplicity of the sum type
figure

Compare the many combinations available in a product type with the restricted alternatives of a sum type. section 7.3

How Write Powerful Rust Macros covers it

How deep it goes
Defines state multiplication with a worked example, then contrasts product types with sum types without implementing product types.
Examples
Uses a struct containing a u8 and boolean, contrasting values such as 120,true and 119,true.
What you can do after
Evaluate how a struct’s field combinations expand its state space and why validation may require conditionals.
How it connects
Connects product types with structs, algebraic data types, sum types, enums, and pattern matching.

Also explained in

Compare how 2 books explain product type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.