122 lines
3.7 KiB
Plaintext
122 lines
3.7 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "742d84cb-79f0-449f-89b7-56d05d074701",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Programming a Guessing Game\n",
|
||
"\n",
|
||
"We’ll implement a classic beginner programming problem: a guessing game. Here’s how it works: the program will generate a random integer between 1 and 100. It will then prompt the player to enter a guess. After a guess is entered, the program will indicate whether the guess is too low or too high. If the guess is correct, the game will print a congratulatory message and exit.\n",
|
||
"\n",
|
||
"### Setting Up a New Project\n",
|
||
"\n",
|
||
"To set up a new *project*, go to the projects directory that you created in Chapter 1 and make a new project using Cargo, like so:\n",
|
||
"\n",
|
||
"```sh\n",
|
||
"$ cargo new guessing_game\n",
|
||
"$ cd guessing_game\n",
|
||
"```\n",
|
||
"\n",
|
||
"The first command, *cargo new*, takes the name of the project (*guessing_game*) as the first argument. The second command changes to the new project’s directory.\n",
|
||
"\n",
|
||
"Look at the generated *Cargo.toml* file:\n",
|
||
"\n",
|
||
"\n",
|
||
"```toml\n",
|
||
"[package]\n",
|
||
"name = \"guessing_game\"\n",
|
||
"version = \"0.1.0\"\n",
|
||
"edition = \"2021\"\n",
|
||
"\n",
|
||
"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n",
|
||
"\n",
|
||
"[dependencies]\n",
|
||
"```\n",
|
||
"\n",
|
||
"As you saw in Chapter 1, `cargo new` generates a “Hello, world!” program for you. Check out the *src/main.rs* file:\n",
|
||
"\n",
|
||
"### Processing a Guess\n",
|
||
"\n",
|
||
"The first part of the guessing game program will ask for user input, process that input, and check that the input is in the expected form. To start, we’ll allow the player to input a guess. Enter the code in Listing 2-1 into *src/main.rs*\n",
|
||
"\n",
|
||
"```rust\n",
|
||
"use std::io;\n",
|
||
"\n",
|
||
"fn main() {\n",
|
||
" println!(\"Guess the number!\");\n",
|
||
"\n",
|
||
" println!(\"Please input your guess.\");\n",
|
||
"\n",
|
||
" let mut guess = String::new();\n",
|
||
"\n",
|
||
" io::stdin()\n",
|
||
" .read_line(&mut guess)\n",
|
||
" .expect(\"Failed to read line\");\n",
|
||
"\n",
|
||
" println!(\"You guessed: {guess}\");\n",
|
||
"}\n",
|
||
"```"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "5222247e-25ef-4e00-8672-0ba63390f3ad",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"use std::io;\n",
|
||
"\n",
|
||
"println!(\"Guess the number!\");\n",
|
||
"println!(\"Please input your guess.\");\n",
|
||
"\n",
|
||
"let mut guess = String::new();\n",
|
||
"\n",
|
||
"io::stdin()\n",
|
||
" .read_line(&mut guess)\n",
|
||
" .expect(\"Failed to read line\");\n",
|
||
"\n",
|
||
"println!(\"You guessed: {guess}\");"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "a7f6d707-2700-4c25-8900-83df60230370",
|
||
"metadata": {},
|
||
"source": [
|
||
"This code contains a lot of information, so let’s go over it line by line. To obtain user input and then print the result as output, we need to bring the `io` input/output library into scope. The `io` library comes from the standard library, known as `std`:\n",
|
||
"\n",
|
||
"```rust\n",
|
||
"use std::io;\n",
|
||
"```"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "c649c49a-2be9-49f4-a30b-cc84f8ba0ebc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Rust",
|
||
"language": "rust",
|
||
"name": "rust"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": "rust",
|
||
"file_extension": ".rs",
|
||
"mimetype": "text/rust",
|
||
"name": "Rust",
|
||
"pygment_lexer": "rust",
|
||
"version": ""
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|