Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D7018E-Rust-Course
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
Fredrik Pettersson
D7018E-Rust-Course
Commits
d80cb763
Commit
d80cb763
authored
7 years ago
by
DevDoggo
Browse files
Options
Downloads
Patches
Plain Diff
Made the result-printing functions into a separate module.
parent
1e82c4bf
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
a2_guessing_game/src/lib.rs
+1
-0
1 addition, 0 deletions
a2_guessing_game/src/lib.rs
a2_guessing_game/src/main.rs
+11
-47
11 additions, 47 deletions
a2_guessing_game/src/main.rs
a2_guessing_game/src/results.rs
+48
-0
48 additions, 0 deletions
a2_guessing_game/src/results.rs
with
60 additions
and
47 deletions
a2_guessing_game/src/lib.rs
0 → 100644
+
1
−
0
View file @
d80cb763
pub
mod
results
;
This diff is collapsed.
Click to expand it.
a2_guessing_game/src/main.rs
+
11
−
47
View file @
d80cb763
extern
crate
rand
;
extern
crate
rand
;
extern
crate
colored
;
extern
crate
colored
;
extern
crate
guessing_game
;
use
std
::
io
;
//standard in-out
use
std
::
io
;
//standard in-out
use
std
::
cmp
::
Ordering
;
use
std
::
cmp
::
Ordering
;
...
@@ -10,6 +11,7 @@ use rand::Rng;
...
@@ -10,6 +11,7 @@ use rand::Rng;
use
std
::
io
::
Write
;
//seemingly needed to use io::stdout().flush()
use
std
::
io
::
Write
;
//seemingly needed to use io::stdout().flush()
use
colored
::
*
;
use
colored
::
*
;
fn
get_input
()
->
Result
<
u32
,
String
>
{
fn
get_input
()
->
Result
<
u32
,
String
>
{
//returns io::Result, this has .expect() method
//returns io::Result, this has .expect() method
let
mut
guess
=
String
::
new
();
let
mut
guess
=
String
::
new
();
...
@@ -26,49 +28,6 @@ fn get_input() -> Result<u32, String> {
...
@@ -26,49 +28,6 @@ fn get_input() -> Result<u32, String> {
}
}
}
}
fn
print_guesses
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
let
iterations
=
2
;
for
_i
in
0
..
iterations
{
println!
(
"
\n
Results: Printing All
\n
Attempt | Guess"
);
for
g
in
v
{
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
}
}
}
fn
print_the_last_three_guesses_in_backwards_order_really_nicely
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
let
mut
count
=
0
;
println!
(
"
\n
Results: Printing Last Three
\n
Attempt | Guess"
);
for
g
in
v
.iter
()
.rev
()
{
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
count
+=
1
;
if
count
==
3
{
break
;
}
}
}
fn
print_hashmap
(
hm
:
&
HashMap
<
u32
,
String
>
)
{
println!
(
"
\n
Results: Printing HashMap
\n
Attempt | Guess"
);
for
m
in
hm
{
println!
(
" {:?} | {:?}"
,
m
.0
,
m
.1
);
}
}
fn
print_hashmap_last_three
(
hm
:
&
HashMap
<
u32
,
String
>
)
{
println!
(
"
\n
Results: Printing Last Three in Hashmap
\n
Attempt | Guess"
);
let
mut
entries
=
0
;
for
_m
in
hm
.iter
()
{
entries
+=
1
;
}
//let ln: u32 = hm.len(); //Give error because hm.len() is usize, not u32
for
i
in
(
max
(
entries
-
2
,
1
)
..
entries
+
1
)
.rev
()
{
let
mut
item
=
hm
.get
(
&
i
);
match
item
{
Some
(
num
)
=>
println!
(
" {} | {}"
,
i
,
num
),
None
=>
println!
(
"Nothing!"
),
};
}
}
fn
main
()
{
fn
main
()
{
println!
(
"{}"
,
"
\n\n
Guess the number or perish. :)"
.bold
());
println!
(
"{}"
,
"
\n\n
Guess the number or perish. :)"
.bold
());
...
@@ -129,9 +88,14 @@ fn main() {
...
@@ -129,9 +88,14 @@ fn main() {
println!
(
"
\n
You've guessed {} time(s) already!"
,
attempts
.to_string
()
.yellow
())
println!
(
"
\n
You've guessed {} time(s) already!"
,
attempts
.to_string
()
.yellow
())
}
}
print_guesses
(
&
guesses
);
//print_guesses(&guesses);
print_the_last_three_guesses_in_backwards_order_really_nicely
(
&
guesses
);
//print_the_last_three_guesses_in_backwards_order_really_nicely(&guesses);
print_hashmap
(
&
guess_hashmap
);
//print_hashmap(&guess_hashmap);
print_hashmap_last_three
(
&
guess_hashmap
);
//print_hashmap_last_three(&guess_hashmap);
guessing_game
::
results
::
print_guesses
(
&
guesses
);
guessing_game
::
results
::
print_the_last_three_guesses_in_backwards_order_really_nicely
(
&
guesses
);
guessing_game
::
results
::
print_hashmap
(
&
guess_hashmap
);
guessing_game
::
results
::
print_hashmap_last_three
(
&
guess_hashmap
);
}
}
This diff is collapsed.
Click to expand it.
a2_guessing_game/src/results.rs
0 → 100644
+
48
−
0
View file @
d80cb763
use
std
::
cmp
::
max
;
use
std
::
collections
::
HashMap
;
pub
fn
print_guesses
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
let
iterations
=
2
;
for
_i
in
0
..
iterations
{
println!
(
"
\n
Results: Printing All
\n
Attempt | Guess"
);
for
g
in
v
{
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
}
}
}
pub
fn
print_the_last_three_guesses_in_backwards_order_really_nicely
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
let
mut
count
=
0
;
println!
(
"
\n
Results: Printing Last Three
\n
Attempt | Guess"
);
for
g
in
v
.iter
()
.rev
()
{
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
count
+=
1
;
if
count
==
3
{
break
;
}
}
}
pub
fn
print_hashmap
(
hm
:
&
HashMap
<
u32
,
String
>
)
{
println!
(
"
\n
Results: Printing HashMap
\n
Attempt | Guess"
);
for
m
in
hm
{
println!
(
" {:?} | {:?}"
,
m
.0
,
m
.1
);
}
}
pub
fn
print_hashmap_last_three
(
hm
:
&
HashMap
<
u32
,
String
>
)
{
println!
(
"
\n
Results: Printing Last Three in Hashmap
\n
Attempt | Guess"
);
let
mut
entries
=
0
;
for
_m
in
hm
.iter
()
{
entries
+=
1
;
}
//let ln: u32 = hm.len(); //Give error because hm.len() is usize, not u32
for
i
in
(
max
(
entries
-
2
,
1
)
..
entries
+
1
)
.rev
()
{
let
mut
item
=
hm
.get
(
&
i
);
match
item
{
Some
(
num
)
=>
println!
(
" {} | {}"
,
i
,
num
),
None
=>
println!
(
"Nothing!"
),
};
}
}
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