Skip to content
This repository has been archived by the owner on Mar 23, 2020. It is now read-only.

Add io assignment #158

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions assignments/IO-assignment/Cargo.toml
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]
55 changes: 55 additions & 0 deletions assignments/IO-assignment/result-option-assignment.adoc
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
* 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.
13 changes: 13 additions & 0 deletions assignments/IO-assignment/solution/Step2.rs
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),
};
}
17 changes: 17 additions & 0 deletions assignments/IO-assignment/solution/Step3.rs
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn main() -> std::io::Result<()> would allow us to use ? instead of unwrap here.

println!("{}", content_string);
}
22 changes: 22 additions & 0 deletions assignments/IO-assignment/solution/Step4.rs
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);
}
24 changes: 24 additions & 0 deletions assignments/IO-assignment/solution/Step5.rs
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);
}
}
39 changes: 39 additions & 0 deletions assignments/IO-assignment/solution/Step6.rs
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);
}
7 changes: 7 additions & 0 deletions assignments/IO-assignment/src/lib/content.txt
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/
12 changes: 12 additions & 0 deletions assignments/IO-assignment/src/main.rs
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,
};
}