This commit is contained in:
2025-03-26 12:41:58 -04:00
parent 637b26a0e9
commit 416dd617e6
36 changed files with 732 additions and 0 deletions

1
example_9/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
example_9/Cargo.toml Normal file
View 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
View 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();
}