Skip to content

Commit de500e8

Browse files
committed
added result
1 parent 6591ca2 commit de500e8

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ Various small examples from my Rust course. Previously a lot
55
of these were on the playground, but I prefer to have them
66
in a controlled repository now.
77

8-
* `datatypes`: examples of various datatype thingies
8+
* `datatypes/`: examples of various datatype thingies
99
* `structs.rs`: struct and enum examples
1010
* `arrays.rs`: array and vector examples
1111
* `strings.rs`: string and str examples
1212
* `iterators.rs`: iterator examples
1313
* `closures.rs`: closure examples
1414
* `lifetimes.rs`: simple lifetime example
15-
* `rc`: examples with `RefCell` and `Rc`
16-
* `readline`: example of readline with error handling
17-
* `error`: example of creating a custom error
18-
* `modules`: example of modules
15+
* `rc/`: examples with `RefCell` and `Rc`
16+
* `readline/`: example of readline with error handling
17+
* `error/`: example of creating a custom error
18+
* `modules/`: example of modules
1919
* `point.rs`: example of "methods" and ownership
2020
* `units.rs`: example of newtype and deriving
2121
* `patmove.rs`: example of `ref` in patterns
22+
* `result/`: example of defining and using `Result`

result/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "result"
3+
version = "0.1.0"
4+
authors = ["Bart Massey <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
11+
[[bin]]
12+
name = "result"
13+
path = "result.rs"
14+
15+
[features]
16+
myerror = []
17+
default_features = []

result/result.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::io::Read;
2+
3+
#[cfg(feature = "ownerror")]
4+
mod my_result {
5+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6+
enum MyResult<T, E> {
7+
Ok(T),
8+
Err(E),
9+
}
10+
11+
impl<T, E> MyResult<T, E> {
12+
fn new(x: Result<T, E>) -> Self {
13+
match x {
14+
Ok(x) => MyResult::Ok(x),
15+
Err(x) => MyResult::Err(x),
16+
}
17+
}
18+
19+
fn unwrap(self) -> T
20+
where E: std::error::Error
21+
{
22+
match self {
23+
MyResult::Ok(v) => v,
24+
MyResult::Err(e) => panic!("unwrap failed: {}", e),
25+
}
26+
}
27+
}
28+
}
29+
30+
fn read_file_as_string<P>(filename: P) -> Result<(usize, String), Box<dyn std::error::Error>>
31+
where P: AsRef<std::path::Path>
32+
{
33+
let mut f = std::fs::File::open(filename.as_ref())?;
34+
let mut bytes: Vec<u8> = Vec::with_capacity(1024);
35+
let n = f.read_to_end(&mut bytes)?;
36+
let s = std::str::from_utf8(&bytes)?;
37+
Ok((n, s.to_string()))
38+
}
39+
40+
fn main() {
41+
match read_file_as_string("/tmp/file") {
42+
Ok((n, s)) => {
43+
println!("{}", n);
44+
print!("{}", s);
45+
}
46+
Err(e) => {
47+
println!("could not read /tmp/file: {}", e);
48+
std::process::exit(1);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)