---
source: https://iinventedlight.com/library/notes/rust-guessing-game.md
title: "Rust book: build a guessing game with input, rand, and match"
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, 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."
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-guessing-game"
tags: [rust, guessing-game, cargo, rand, match]
client: Librarian
sources:
  - id: S1
    title: "Programming a Guessing Game"
    publisher: The Rust Project Developers
    url: https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html
    accessed: 2026-11-27
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "Programming a Guessing Game (source)"
    publisher: The Rust Project Developers
    url: https://raw.githubusercontent.com/rust-lang/book/main/src/ch02-00-guessing-game-tutorial.md
    accessed: 2026-11-27
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-variables-and-mutability](https://iinventedlight.com/library/notes/rust-variables-and-mutability/)
- [rust-functions](https://iinventedlight.com/library/notes/rust-functions/)
- [rust-match](https://iinventedlight.com/library/notes/rust-match/)

## Claims

- id: C1
  claim: The game picks a random integer from 1 to 100 and tells the player if a guess is high or low.
  source: S1
  evidence: "a random integer between 1 and 100"

- id: C2
  claim: Rust variables are immutable by default.
  source: S2
  evidence: "variables are immutable by default"

- id: C3
  claim: cargo new generates a Hello, world! program.
  source: S2
  evidence: "generates a \"Hello, world!\" program"

## Sources

- [S1] Programming a Guessing Game. The Rust Project Developers. https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html . License: MIT OR Apache-2.0. Accessed 2026-11-27.

- [S2] Programming a Guessing Game (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch02-00-guessing-game-tutorial.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, 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.

---

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