Add code
This commit is contained in:
1
example_9/.gitignore
vendored
Normal file
1
example_9/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
example_9/Cargo.toml
Normal file
7
example_9/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "example_9"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
51
example_9/src/main.rs
Normal file
51
example_9/src/main.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use anyhow::{Context, Result};
|
||||
use std::num::NonZero;
|
||||
|
||||
enum CustomOption<T> {
|
||||
Some(T),
|
||||
None,
|
||||
}
|
||||
enum CustomResult<T, E> {
|
||||
Ok(T),
|
||||
Err(E),
|
||||
}
|
||||
|
||||
struct CustomVec {}
|
||||
|
||||
impl CustomVec {
|
||||
fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Findable<T> {
|
||||
fn find(&self, fun: T) -> Option<u32>;
|
||||
}
|
||||
|
||||
impl<F> Findable<F> for CustomVec
|
||||
where
|
||||
F: Fn(i32) -> bool,
|
||||
{
|
||||
fn find(&self, fun: F) -> Option<u32> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
fn do_stuff() -> Result<NonZero<i32>> {
|
||||
let my_vec = CustomVec::new();
|
||||
|
||||
// v is Option<&T>, not &T -- cannot use without checking for None
|
||||
let v = my_vec.find(|t| t >= 42);
|
||||
|
||||
// n is Result<i32, ParseIntError> -- cannot use without checking for Err
|
||||
let n = "42".parse::<u32>();
|
||||
|
||||
// ? suffix is "return Err if Err, otherwise unwrap Ok"
|
||||
let n = "42".parse::<u32>()?;
|
||||
|
||||
let n = NonZero::new(43).context("oopie")?;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
do_stuff();
|
||||
}
|
||||
Reference in New Issue
Block a user