Skip to content

Commit 1d6cf74

Browse files
committed
finalize guessing_game
1 parent 9b7d2a7 commit 1d6cf74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+59
-22
lines changed

guessing_game/src/main.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@ fn main() {
77

88
let secret_number = rand::thread_rng().gen_range(1..=100);
99

10-
println!("The secret number is: {secret_number}");
11-
12-
println!("Please input your guess.");
13-
14-
let mut guess = String::new();
15-
16-
io::stdin()
17-
.read_line(&mut guess)
18-
.expect("Failed to read line");
19-
20-
let guess: u32 = guess.trim().parse().expect("Please type a number!");
21-
22-
println!("You guessed: {guess}");
23-
24-
match guess.cmp(&secret_number) {
25-
Ordering::Less => println!("Too small!"),
26-
Ordering::Greater => println!("Too big!"),
27-
Ordering::Equal => println!("You win!"),
10+
//println!("The secret number is: {secret_number}");
11+
12+
loop {
13+
println!("Please input your guess.");
14+
15+
let mut guess = String::new();
16+
17+
io::stdin()
18+
.read_line(&mut guess)
19+
.expect("Failed to read line");
20+
21+
let guess: u32 = match guess.trim().parse() {
22+
Ok(num) => num,
23+
Err(_) => continue,
24+
};
25+
26+
println!("You guessed: {guess}");
27+
28+
match guess.cmp(&secret_number) {
29+
Ordering::Less => println!("Too small!"),
30+
Ordering::Greater => println!("Too big!"),
31+
Ordering::Equal => {
32+
println!("You win!");
33+
break;
34+
}
35+
}
2836
}
37+
2938
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
792486d3db73efab
1+
0b793a66eb8d87a8
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"rustc":16065624160775160083,"features":"[]","target":18246892274002150725,"profile":1139431570667407349,"path":1684066648322511884,"deps":[[5910892534286594076,"rand",false,634987732658045247],[5910892534286594076,"rand",false,13160459092054503461]],"local":[{"Precalculated":"13326482844.579927700s (target/.rustdoc_fingerprint.json)"}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
1+
{"rustc":16065624160775160083,"features":"[]","target":18246892274002150725,"profile":1139431570667407349,"path":1684066648322511884,"deps":[[5910892534286594076,"rand",false,634987732658045247],[5910892534286594076,"rand",false,13160459092054503461]],"local":[{"Precalculated":"13326484398.481710400s (target/debug/guessing_game.d)"}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
-512 Bytes
Binary file not shown.
Binary file not shown.
-512 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `main` fn in crate `guessing_game`."><meta name="keywords" content="rust, rustlang, rust-lang, main"><title>main in guessing_game - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.js"></script><script defer src="../main.js"></script>
2-
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../guessing_game/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><div class="sidebar-elems"><h2 class="location">Other items in<br><a href="index.html">guessing_game</a></h2><div id="sidebar-vars" data-name="main" data-ty="fn" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Function <a href="index.html">guessing_game</a>::<wbr><a class="fn" href="#">main</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span><a class="srclink" href="../src/guessing_game/main.rs.html#3-15" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust fn"><code>pub(crate) fn main()</code></pre></div></section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="guessing_game" data-search-index-js="../search-index.js" data-search-js="../search.js"></div>
2+
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../guessing_game/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><div class="sidebar-elems"><h2 class="location">Other items in<br><a href="index.html">guessing_game</a></h2><div id="sidebar-vars" data-name="main" data-ty="fn" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Function <a href="index.html">guessing_game</a>::<wbr><a class="fn" href="#">main</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span><a class="srclink" href="../src/guessing_game/main.rs.html#5-29" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust fn"><code>pub(crate) fn main()</code></pre></div></section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="guessing_game" data-search-index-js="../search-index.js" data-search-js="../search.js"></div>
33
</body></html>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `guessing_game` crate."><meta name="keywords" content="rust, rustlang, rust-lang, guessing_game"><title>guessing_game - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.js"></script><script defer src="../main.js"></script>
2-
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../guessing_game/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><h2 class="location">Crate guessing_game</h2><div class="block version"><div class="narrow-helper"></div><p>Version 0.1.0</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all guessing_game's items</p></a><div class="block items"><ul><li><a href="#functions">Functions</a></li></ul></div><div id="sidebar-vars" data-name="guessing_game" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">guessing_game</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span><a class="srclink" href="../src/guessing_game/main.rs.html#1-15" title="goto source code">[src]</a></span></h1><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2>
2+
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../guessing_game/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><h2 class="location">Crate guessing_game</h2><div class="block version"><div class="narrow-helper"></div><p>Version 0.1.0</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all guessing_game's items</p></a><div class="block items"><ul><li><a href="#functions">Functions</a></li></ul></div><div id="sidebar-vars" data-name="guessing_game" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">guessing_game</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span><a class="srclink" href="../src/guessing_game/main.rs.html#1-29" title="goto source code">[src]</a></span></h1><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2>
33
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.main.html" title="guessing_game::main fn">main</a></div><div class="item-right docblock-short"></div></div></div></section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="guessing_game" data-search-index-js="../search-index.js" data-search-js="../search.js"></div>
44
</body></html>

guessing_game/target/doc/src/guessing_game/main.rs.html

+28
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,31 @@
1414
<span id="13">13</span>
1515
<span id="14">14</span>
1616
<span id="15">15</span>
17+
<span id="16">16</span>
18+
<span id="17">17</span>
19+
<span id="18">18</span>
20+
<span id="19">19</span>
21+
<span id="20">20</span>
22+
<span id="21">21</span>
23+
<span id="22">22</span>
24+
<span id="23">23</span>
25+
<span id="24">24</span>
26+
<span id="25">25</span>
27+
<span id="26">26</span>
28+
<span id="27">27</span>
29+
<span id="28">28</span>
30+
<span id="29">29</span>
1731
</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">std::io</span>;
32+
<span class="kw">use</span> <span class="ident">std::cmp::Ordering</span>;
33+
<span class="kw">use</span> <span class="ident">rand::Rng</span>;
1834

1935
<span class="kw">fn</span> <span class="ident">main</span>() {
2036
<span class="macro">println!</span>(<span class="string">&quot;Guess the number!&quot;</span>);
2137

38+
<span class="kw">let</span> <span class="ident">secret_number</span> <span class="op">=</span> <span class="ident">rand::thread_rng</span>().<span class="ident">gen_range</span>(<span class="number">1</span>..<span class="op">=</span><span class="number">100</span>);
39+
40+
<span class="macro">println!</span>(<span class="string">&quot;The secret number is: {secret_number}&quot;</span>);
41+
2242
<span class="macro">println!</span>(<span class="string">&quot;Please input your guess.&quot;</span>);
2343

2444
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">guess</span> <span class="op">=</span> <span class="ident">String::new</span>();
@@ -27,7 +47,15 @@
2747
.<span class="ident">read_line</span>(<span class="kw-2">&amp;mut</span> <span class="ident">guess</span>)
2848
.<span class="ident">expect</span>(<span class="string">&quot;Failed to read line&quot;</span>);
2949

50+
<span class="kw">let</span> <span class="ident">guess</span>: <span class="ident">u32</span> <span class="op">=</span> <span class="ident">guess</span>.<span class="ident">trim</span>().<span class="ident">parse</span>().<span class="ident">expect</span>(<span class="string">&quot;Please type a number!&quot;</span>);
51+
3052
<span class="macro">println!</span>(<span class="string">&quot;You guessed: {guess}&quot;</span>);
53+
54+
<span class="kw">match</span> <span class="ident">guess</span>.<span class="ident">cmp</span>(<span class="kw-2">&amp;</span><span class="ident">secret_number</span>) {
55+
<span class="ident">Ordering::Less</span> =&gt; <span class="macro">println!</span>(<span class="string">&quot;Too small!&quot;</span>),
56+
<span class="ident">Ordering::Greater</span> =&gt; <span class="macro">println!</span>(<span class="string">&quot;Too big!&quot;</span>),
57+
<span class="ident">Ordering::Equal</span> =&gt; <span class="macro">println!</span>(<span class="string">&quot;You win!&quot;</span>),
58+
}
3159
}
3260
</code></pre></div>
3361
</section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../../" data-current-crate="guessing_game" data-search-index-js="../../search-index.js" data-search-js="../../search.js"></div>

0 commit comments

Comments
 (0)