---
source: https://iinventedlight.com/library/notes/rust-example-structs.md
title: "Rust book: refactor a rectangle area program into a struct"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Project Developers); MIT OR Apache-2.0 (The Rust Project Developers); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, An Example Program Using Structs; The Rust Project Developers, An Example Program Using Structs (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: rust-example-structs"
tags: [rust, structs, rectangle, debug, dbg]
client: Librarian
sources:
  - id: S1
    title: "An Example Program Using Structs"
    publisher: The Rust Project Developers
    url: https://doc.rust-lang.org/book/ch05-02-example-structs.html
    accessed: 2026-11-27
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "An Example Program Using Structs (source)"
    publisher: The Rust Project Developers
    url: https://raw.githubusercontent.com/rust-lang/book/main/src/ch05-02-example-structs.md
    accessed: 2026-11-27
    license: "MIT OR Apache-2.0"
---
# Rust book: refactor a rectangle area program into a struct
**Answer:** This chapter uses a rectangles program to show when to use structs.

## From variables to a Rectangle type

The example starts with single variables and then refactors until a struct is in use.

The sample rectangles project computes area from width and height in pixels.

An area function with two parameters does not show that those values belong together as one rectangle.

A tuple adds structure and a single argument, but the elements have no names, so you index into the parts.

Mixing width and height would not matter for the area calculation, but it would matter if we wanted to draw the rectangle on the screen.

A struct adds meaning by labeling the data. Rectangle has width and height fields, both of type u32.

area then takes an immutable borrow of Rectangle so main keeps ownership.

## Debug output

Putting :? inside the curly brackets of println! requests the Debug output format, meant for developers.

You have to opt a struct into debug printing, usually with #[derive(Debug)] before the definition.

For larger structs, {:#?} is easier to read.

The dbg! macro also prints with Debug formatting. It reports the file, line, and value, and it writes to standard error.

You can put dbg! around 30 * scale. Because dbg! returns the value of the expression, you get the same value.

## See also

- [rust-structs](https://iinventedlight.com/library/notes/rust-structs/)
- [rust-tuples](https://iinventedlight.com/library/notes/rust-tuples/)
- [rust-method-syntax](https://iinventedlight.com/library/notes/rust-method-syntax/)

## Claims

- id: C1
  claim: The chapter starts from loose width and height variables and refactors toward a struct.
  source: S1
  evidence: "We'll start by using single variables and then refactor the program"

- id: C2
  claim: Tuples group the values but do not name their elements.
  source: S2
  evidence: "Tuples don’t name their elements, so we have to index"

- id: C3
  claim: Adding #[derive(Debug)] opts the struct into debug printing.
  source: S2
  evidence: "we add the outer attribute `#[derive(Debug)]` just before the"

## Sources

- [S1] An Example Program Using Structs. The Rust Project Developers. https://doc.rust-lang.org/book/ch05-02-example-structs.html . License: MIT OR Apache-2.0. Accessed 2026-11-27.

- [S2] An Example Program Using Structs (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch05-02-example-structs.md . License: MIT OR Apache-2.0. Accessed 2026-11-27.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, An Example Program Using Structs; The Rust Project Developers, An Example Program Using Structs (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
