Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Rust_lekstuga
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Anders Nilsson
Rust_lekstuga
Commits
8a5c2117
Commit
8a5c2117
authored
9 years ago
by
Anders Nilsson
Browse files
Options
Downloads
Patches
Plain Diff
Sync.
parent
04e6450d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
city-pop/src/main.rs
+15
-7
15 additions, 7 deletions
city-pop/src/main.rs
with
15 additions
and
7 deletions
city-pop/src/main.rs
+
15
−
7
View file @
8a5c2117
...
@@ -4,8 +4,9 @@ extern crate csv;
...
@@ -4,8 +4,9 @@ extern crate csv;
use
getopts
::
Options
;
use
getopts
::
Options
;
use
std
::{
env
,
path
,
fs
};
use
std
::{
env
,
fs
};
use
std
::
error
::
Error
;
use
std
::
error
::
Error
;
use
std
::
path
::
Path
;
// This struct represents the data in each row of the CSV file.
// This struct represents the data in each row of the CSV file.
// Type based decoding absolves us of a lot of the nitty gritty error
// Type based decoding absolves us of a lot of the nitty gritty error
...
@@ -42,10 +43,10 @@ struct PopulationCount {
...
@@ -42,10 +43,10 @@ struct PopulationCount {
//impl From<String> for Box<Error + Send + Sync>
//impl From<String> for Box<Error + Send + Sync>
fn
print_usage
(
program
:
&
str
,
opts
:
Options
)
{
fn
print_usage
(
program
:
&
str
,
opts
:
Options
)
{
println!
(
"{}"
,
opts
.usage
(
&
format!
(
"Usage: {} [options]
<data-path>
<city>"
,
program
)));
println!
(
"{}"
,
opts
.usage
(
&
format!
(
"Usage: {} [options] <city>"
,
program
)));
}
}
fn
search
<
P
:
AsRef
<
path
::
Path
>>
(
file_path
:
P
,
city
:
&
str
)
fn
search
<
P
:
AsRef
<
Path
>>
(
file_path
:
&
Option
<
P
>
,
city
:
&
str
)
->
Result
<
Vec
<
PopulationCount
>
,
Box
<
Error
+
Send
+
Sync
>>
{
->
Result
<
Vec
<
PopulationCount
>
,
Box
<
Error
+
Send
+
Sync
>>
{
let
mut
found
=
vec!
[];
let
mut
found
=
vec!
[];
let
file
=
try
!
(
fs
::
File
::
open
(
file_path
));
let
file
=
try
!
(
fs
::
File
::
open
(
file_path
));
...
@@ -75,6 +76,7 @@ fn main() {
...
@@ -75,6 +76,7 @@ fn main() {
let
program
=
args
[
0
]
.clone
();
let
program
=
args
[
0
]
.clone
();
let
mut
opts
=
Options
::
new
();
let
mut
opts
=
Options
::
new
();
opts
.optopt
(
"f"
,
"file"
,
"Choose an input file, instead of using STDIN."
,
"NAME"
);
opts
.optflag
(
"h"
,
"help"
,
"Show this usage message."
);
opts
.optflag
(
"h"
,
"help"
,
"Show this usage message."
);
let
matches
=
match
opts
.parse
(
&
args
[
1
..
])
{
let
matches
=
match
opts
.parse
(
&
args
[
1
..
])
{
...
@@ -86,11 +88,17 @@ fn main() {
...
@@ -86,11 +88,17 @@ fn main() {
return
;
return
;
}
}
let
data_file
=
args
[
1
]
.clone
();
let
file
=
matches
.opt_str
(
"f"
);
let
data_path
=
path
::
Path
::
new
(
&
data_file
);
let
data_file
=
file
.as_ref
()
.map
(
Path
::
new
);
let
city
=
args
[
2
]
.clone
();
for
pop
in
search
(
&
data_path
,
&
city
)
.unwrap
()
{
let
city
=
if
!
matches
.free
.is_empty
()
{
matches
.free
[
0
]
.clone
()
}
else
{
print_usage
(
&
program
,
opts
);
return
;
};
for
pop
in
search
(
&
data_file
,
&
city
)
.unwrap
()
{
println!
(
"{}, {}: {:?}"
,
pop
.city
,
pop
.country
,
pop
.count
);
println!
(
"{}, {}: {:?}"
,
pop
.city
,
pop
.country
,
pop
.count
);
}
}
...
...
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