← All notes

Rust book: build a guessing game with input, rand, and match

Answer: Chapter 2 builds a guessing game to practice let, match, methods, associated functions, and an external crate.

The game and the project

The program generates a random integer between 1 and 100 and says whether a guess is too low or too high. A correct guess prints a congratulatory message and exits.

Create the project with cargo new guessing_game and enter that directory. cargo new writes a Hello, world! program, and cargo run compiles and runs it.

Code goes in src/main.rs.

Reading a guess

The first part of the guessing game program will ask for user input. If a type is not in the prelude, bring it into scope with use, for example use std::io.

main is the entry point. println! prints a prompt that asks for a guess.

In Rust, variables are immutable by default. let mut guess = String::new(); creates a mutable String. The ::new syntax indicates that new is an associated function.

The read_line method reads a line of input. The & indicates that this argument is a reference. The method returns a Result. If this instance of Result is an Ok value, expect will take the return value. An Err value crashes the program with that message.

Println! prints the string that contains the user's input. Curly brackets mark a placeholder in that string.

The secret number

A crate is a collection of Rust source files. Adding rand under [dependencies] lets Cargo fetch it.

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, Programming a Guessing Game; The Rust Project Developers, Programming a Guessing Game (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, Programming a Guessing Game; The Rust Project Developers, Programming a Guessing Game (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:yvwxp8xf

iinventedlight. 2026. "Rust book: build a guessing game with input, rand, and match." iinventedlight Library, iil:note:yvwxp8xf, version of 2026-11-27. https://iinventedlight.com/library/notes/rust-guessing-game/. License: CC-BY-4.0.

@misc{iil_note_yvwxp8xf,
  author = {{iinventedlight}},
  title = {{Rust book: build a guessing game with input, rand, and match}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/rust-guessing-game/},
  note = {Permanent ID iil:note:yvwxp8xf. 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-guessing-game.md.