liveBook · Manning

Tuple struct

As explained in Rust in Action: Systems programming concepts and techniques

A tuple struct is a Rust struct whose fields are unnamed and accessed by position.

A tuple struct is a Rust struct with unnamed fields. Define one by placing field types in parentheses, as in `pub struct Q7(i8);`. Positional fields keep the representation concise while allowing the type to carry its own behavior and derived traits. section 5.5

Q7 fixed-point value

`Q7(i8)` stores a compact fixed-point value. Deriving `Debug`, `Clone`, `Copy`, `PartialEq`, and `Eq` supplies formatting, duplication, copying, and comparison. Conversion code accesses the internal field as `n.0`, while `From` implementations convert between Q7 and `f32` or `f64`. section 5.5

Listing 5.11 Definition of the Q7 format
#[derive(Debug,Clone,Copy,PartialEq,Eq)]
pub struct Q7(i8);                          #1

Inspect the minimal tuple-struct declaration and its derived traits before following the complete Q7 conversion implementation. section 5.5

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains declaration syntax, positional access, multiple fields, derived traits, and a complete conversion example.
Examples
Demonstrates `Q7(i8)`, derived traits, positional access, floating-point conversions, and conversion tests.
What you can do after
Build it from scratch by defining a tuple struct, deriving traits, and implementing conversions around its stored value.
How it connects
Connects tuple structs with structs, traits, ownership, fixed-point numbers, and type conversion.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.