Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
a2_guessing_game
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
D7018E-SS-RUST
a2_guessing_game
Commits
16b76d6e
Commit
16b76d6e
authored
7 years ago
by
Henrik Tjäder
Browse files
Options
Downloads
Patches
Plain Diff
Do the same but with HashMaps
parent
19df3cce
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main.rs
+8
-11
8 additions, 11 deletions
src/main.rs
with
8 additions
and
11 deletions
src/main.rs
+
8
−
11
View file @
16b76d6e
extern
crate
rand
;
extern
crate
rand
;
use
std
::
io
;
use
std
::
io
;
use
std
::
vec
::
Vec
;
use
std
::
collections
::
HashMap
;
use
std
::
cmp
::
Ordering
;
use
std
::
cmp
::
Ordering
;
use
rand
::
Rng
;
use
rand
::
Rng
;
...
@@ -11,7 +11,7 @@ fn main() {
...
@@ -11,7 +11,7 @@ fn main() {
let
secret_number
=
rand
::
thread_rng
()
.gen_range
(
1
,
101
);
let
secret_number
=
rand
::
thread_rng
()
.gen_range
(
1
,
101
);
let
mut
tries_counter
:
u32
=
0
;
let
mut
tries_counter
:
u32
=
0
;
let
mut
attempt_history
=
Vec
::
<
(
u32
,
String
)
>
::
new
();
let
mut
attempt_history
=
HashMap
::
new
();
println!
(
"The secret number is: {}"
,
secret_number
);
println!
(
"The secret number is: {}"
,
secret_number
);
...
@@ -31,7 +31,9 @@ fn main() {
...
@@ -31,7 +31,9 @@ fn main() {
tries_counter
+=
1
;
tries_counter
+=
1
;
attempt_history
.push
((
tries_counter
,
format!
(
"{}"
,
guess
)));
//attempt_history.push((tries_counter, format!("{}", guess)));
attempt_history
.insert
(
tries_counter
,
format!
(
"{}"
,
guess
));
match
guess
.cmp
(
&
secret_number
)
{
match
guess
.cmp
(
&
secret_number
)
{
Ordering
::
Less
=>
println!
(
"Too small!"
),
Ordering
::
Less
=>
println!
(
"Too small!"
),
...
@@ -39,16 +41,11 @@ fn main() {
...
@@ -39,16 +41,11 @@ fn main() {
Ordering
::
Equal
=>
{
Ordering
::
Equal
=>
{
println!
(
"You win!"
);
println!
(
"You win!"
);
println!
(
"Total number of attempts: {}"
,
tries_counter
);
println!
(
"Total number of attempts: {}"
,
tries_counter
);
println!
(
"Last entry first, maximum 3:"
);
for
x
in
attempt_history
{
for
(
i
,
x
)
in
attempt_history
.iter
()
.rev
()
.enumerate
()
{
println!
(
"{:?}"
,
x
);
println!
(
"{:?}"
,
x
);
// Only print the 3 last (see .rev() above)
if
i
>=
2
{
break
;
}
}
}
println!
(
"Since the HashMap does not push things onto a heap like
the Vec-type does, it will iterate over and print in the order items are found"
);
break
;
break
;
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment