diff --git a/assignments/IO-assignment/Cargo.toml b/assignments/IO-assignment/Cargo.toml new file mode 100644 index 0000000..240a544 --- /dev/null +++ b/assignments/IO-assignment/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "option-result-assignment" +version = "0.1.0" +authors = ["Mirabellensaft "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/assignments/IO-assignment/result-option-assignment.adoc b/assignments/IO-assignment/result-option-assignment.adoc new file mode 100644 index 0000000..1ffcb24 --- /dev/null +++ b/assignments/IO-assignment/result-option-assignment.adoc @@ -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()` 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()`, 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. diff --git a/assignments/IO-assignment/solution/Step2.rs b/assignments/IO-assignment/solution/Step2.rs new file mode 100644 index 0000000..8d5606a --- /dev/null +++ b/assignments/IO-assignment/solution/Step2.rs @@ -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), + }; +} diff --git a/assignments/IO-assignment/solution/Step3.rs b/assignments/IO-assignment/solution/Step3.rs new file mode 100644 index 0000000..ba0c5c2 --- /dev/null +++ b/assignments/IO-assignment/solution/Step3.rs @@ -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(); + println!("{}", content_string); +} diff --git a/assignments/IO-assignment/solution/Step4.rs b/assignments/IO-assignment/solution/Step4.rs new file mode 100644 index 0000000..b5868c6 --- /dev/null +++ b/assignments/IO-assignment/solution/Step4.rs @@ -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); +} diff --git a/assignments/IO-assignment/solution/Step5.rs b/assignments/IO-assignment/solution/Step5.rs new file mode 100644 index 0000000..8fe67a2 --- /dev/null +++ b/assignments/IO-assignment/solution/Step5.rs @@ -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); + } +} diff --git a/assignments/IO-assignment/solution/Step6.rs b/assignments/IO-assignment/solution/Step6.rs new file mode 100644 index 0000000..ac7bbda --- /dev/null +++ b/assignments/IO-assignment/solution/Step6.rs @@ -0,0 +1,39 @@ +use std::io::{Read, BufReader, BufRead, Lines}; +use std::fs::File; + +fn is_line_empty(line: String) -> Option { + 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); +} diff --git a/assignments/IO-assignment/src/lib/content.txt b/assignments/IO-assignment/src/lib/content.txt new file mode 100644 index 0000000..4955b2c --- /dev/null +++ b/assignments/IO-assignment/src/lib/content.txt @@ -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/ diff --git a/assignments/IO-assignment/src/main.rs b/assignments/IO-assignment/src/main.rs new file mode 100644 index 0000000..d2585cd --- /dev/null +++ b/assignments/IO-assignment/src/main.rs @@ -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, + }; +}