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
04e6450d
Commit
04e6450d
authored
9 years ago
by
Anders Nilsson
Browse files
Options
Downloads
Patches
Plain Diff
More error handling
parent
bc40f115
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
city-pop/src/main.rs
+18
-4
18 additions, 4 deletions
city-pop/src/main.rs
with
18 additions
and
4 deletions
city-pop/src/main.rs
+
18
−
4
View file @
04e6450d
...
...
@@ -5,6 +5,7 @@ extern crate csv;
use
getopts
::
Options
;
use
std
::{
env
,
path
,
fs
};
use
std
::
error
::
Error
;
// 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
...
...
@@ -32,13 +33,22 @@ struct PopulationCount {
count
:
u64
,
}
// We are making use of this impl in the code above, since we call `From::from`
// on a `&'static str`.
//impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a>
// But this is also useful when you need to allocate a new string for an
// error message, usually with `format!`.
//impl From<String> for Box<Error + Send + Sync>
fn
print_usage
(
program
:
&
str
,
opts
:
Options
)
{
println!
(
"{}"
,
opts
.usage
(
&
format!
(
"Usage: {} [options] <data-path> <city>"
,
program
)));
}
fn
search
<
P
:
AsRef
<
path
::
Path
>>
(
file_path
:
P
,
city
:
&
str
)
->
Vec
<
PopulationCount
>
{
fn
search
<
P
:
AsRef
<
path
::
Path
>>
(
file_path
:
P
,
city
:
&
str
)
->
Result
<
Vec
<
PopulationCount
>
,
Box
<
Error
+
Send
+
Sync
>>
{
let
mut
found
=
vec!
[];
let
file
=
fs
::
File
::
open
(
file_path
)
.unwrap
(
);
let
file
=
try
!
(
fs
::
File
::
open
(
file_path
));
let
mut
rdr
=
csv
::
Reader
::
from_reader
(
file
);
for
row
in
rdr
.decode
::
<
Row
>
()
{
let
row
=
row
.unwrap
();
...
...
@@ -53,7 +63,11 @@ fn search<P: AsRef<path::Path>>(file_path: P, city: &str) -> Vec<PopulationCount
},
}
}
found
if
found
.is_empty
()
{
Err
(
From
::
from
(
"No matching cities with a population were found."
))
}
else
{
Ok
(
found
)
}
}
fn
main
()
{
...
...
@@ -76,7 +90,7 @@ fn main() {
let
data_path
=
path
::
Path
::
new
(
&
data_file
);
let
city
=
args
[
2
]
.clone
();
for
pop
in
search
(
&
data_path
,
&
city
)
{
for
pop
in
search
(
&
data_path
,
&
city
)
.unwrap
()
{
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