This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Add io assignment #158
Draft
Mirabellensaft
wants to merge
5
commits into
master
Choose a base branch
from
add-io-assignment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add io assignment #158
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "option-result-assignment" | ||
version = "0.1.0" | ||
authors = ["Mirabellensaft <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
= Exercise The Option and Result Type and URL parsing | ||
|
||
:icons: font | ||
:source-highlighter: pygments | ||
:pygments-style: borland | ||
|
||
:source-language: rust | ||
|
||
In this exercise, you will learn | ||
* how to handle errors using the `Result`-type. | ||
* how to use the `Option`-type. | ||
* how to read a file line by line. | ||
* how to count the number of lines in a file. | ||
|
||
|
||
Both types are similar in the way, that they can have two types of values, and | ||
depending on what those values are, the program continues in a different way. | ||
|
||
The Option Type can have the variant `Some(<some other type>)` or `None`. | ||
It is used, when you have to handle optional values, for example if you want to | ||
be able to leave a field of a struct empty, go assign the option type to it. | ||
If the field has a value, it is `Some(<value>)`, if it is empty, it is `None`. | ||
|
||
The variants of the Result type are `Ok()` and `Err(e)`. It is used to handle errors. | ||
If an operation was successful, `Ok()` is returned. `Ok()` can be empty or have a | ||
return value. `Err(e)` contains an error message that can be printed. | ||
|
||
# `match` | ||
|
||
Both types can be used with the `match` keyword. The received value is matched on patterns, each leads to the execution of a different expression. | ||
|
||
--- | ||
match VALUE { | ||
PATTERN => EXPRESSION, | ||
PATTERN => EXPRESSION, | ||
PATTERN => EXPRESSION, | ||
} | ||
--- | ||
== Tasks | ||
|
||
* Find out, what type the variable f is. Either your IDE shows you, or you can assign a random type to the variable, and run the program. | ||
* Match the two possible patterns, `Ok(file)` and `Err(e)` to an an appropriate expression, for example: `println!("File opened")` and `println!("Error: {}", e)` | ||
* To use the content of the file, bind the `match` statement to a variable. Read the content of the file into a buffer and then into a `String`. | ||
* Print the entire content of the file. | ||
* Print the number of lines the file contains. | ||
* Print the content of the file line by line. The `lines()`- method returns the `Result`-Type, use it. | ||
* Use the `Option`- type to filter out the empty lines, and only print the the others. | ||
|
||
== Help (wip) | ||
|
||
|
||
Read | ||
3. If `match` is bound to a variable, both arms of the tree have to have the same type, except if one has a `continue` statement. | ||
4. Read | ||
5. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::io::{Read, BufReader, BufRead, Lines}; | ||
use std::fs::File; | ||
|
||
|
||
fn main() { | ||
|
||
let f = File::open("src/lib/content.txt"); | ||
|
||
match f { | ||
Ok(file) => println!("{}"), | ||
Err(e) => panic!("Problem opening the file: {:?}", e), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::io::{Read, BufReader, BufRead, Lines}; | ||
use std::fs::File; | ||
|
||
fn main() { | ||
|
||
let f = File::open("src/lib/content.txt"); | ||
|
||
let file = match f { | ||
Ok(file) => file, | ||
Err(e) => panic!("Problem opening the file: {:?}", e), | ||
}; | ||
|
||
let mut buf_reader = BufReader::new(file); | ||
let mut content_string = String::new(); | ||
buf_reader.read_to_string(&mut content_string).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
println!("{}", content_string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::io::{Read, BufReader, BufRead, Lines}; | ||
use std::fs::File; | ||
|
||
fn main() { | ||
|
||
let f = File::open("src/lib/content.txt"); | ||
|
||
let file = match f { | ||
Ok(file) => file, | ||
Err(e) => panic!("Problem opening the file: {:?}", e), | ||
}; | ||
|
||
let mut buf_reader = BufReader::new(file).lines(); | ||
|
||
let mut number = 0; | ||
|
||
for line in buf_reader { | ||
number += 1; | ||
} | ||
|
||
println!("{}", number); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::io::{Read, BufReader, BufRead, Lines}; | ||
use std::fs::File; | ||
|
||
fn main() { | ||
|
||
let f = File::open("src/lib/content.txt"); | ||
|
||
let file = match f { | ||
Ok(file) => file, | ||
Err(e) => panic!("Problem opening the file: {:?}", e), | ||
}; | ||
|
||
let mut buf_reader = BufReader::new(file).lines(); | ||
|
||
for line in buf_reader { | ||
|
||
let line = match line { | ||
Ok(content) => content, | ||
Err(e) => panic!("Problem reading the line: {:?}", e), | ||
}; | ||
|
||
println!("{}", line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::io::{Read, BufReader, BufRead, Lines}; | ||
use std::fs::File; | ||
|
||
fn is_line_empty(line: String) -> Option<String> { | ||
if line.len() == 0 { | ||
None | ||
} else { | ||
Some(line) | ||
} | ||
} | ||
|
||
fn main() { | ||
|
||
let f = File::open("src/lib/content.txt"); | ||
|
||
let file = match f { | ||
Ok(file) => file, | ||
Err(e) => panic!("Problem opening the file: {:?}", e), | ||
}; | ||
|
||
|
||
let mut buf_reader = BufReader::new(file).lines(); | ||
|
||
for line in buf_reader { | ||
|
||
let line = match line { | ||
Ok(content) => content, | ||
Err(e) => panic!("Problem opening the file: {:?}", e), | ||
}; | ||
|
||
let line = is_line_empty(line); | ||
|
||
match line { | ||
Some(line) => println!("{}", line), | ||
None => continue | ||
}; | ||
} | ||
println!("{}", line); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
This file contains a title and list of URLs and empty lines | ||
https://docs.rs/lyon_core/0.8.0/lyon_core/ | ||
|
||
https://docs.rs/gdnative-core/0.7.0/gdnative_core/ | ||
|
||
https://docs.rs/little-endian/1.0.0/little_endian/ | ||
https://docs.rs/grin_keychain/3.0.0/grin_keychain/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use std::fs::File; | ||
|
||
fn main() { | ||
|
||
let f = File::open("src/lib/content.txt"); | ||
|
||
match f { | ||
// substitute this placeholder for the two possible patterns from the result type | ||
// PATTERN => EXPRESSION, | ||
// PATTERN => EXPRESSION, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.