← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:hcbw95bv

iinventedlight. 2026. "Rust book: refactor a rectangle area program into a struct." iinventedlight Library, iil:note:hcbw95bv, version of 2026-11-27. https://iinventedlight.com/library/notes/rust-example-structs/. License: CC-BY-4.0.

@misc{iil_note_hcbw95bv,
  author = {{iinventedlight}},
  title = {{Rust book: refactor a rectangle area program into a struct}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/rust-example-structs/},
  note = {Permanent ID iil:note:hcbw95bv. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-example-structs.md.