Skip to content

Commit 652c8c5

Browse files
committed
add custom option impl methods
1 parent 7e5586e commit 652c8c5

File tree

1 file changed

+187
-1
lines changed

1 file changed

+187
-1
lines changed

src/main.rs

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@
19201920
// No. of ref incremented to +1 - 2
19211921
// let truck2 = Rc::clone(&truck1);
19221922
// println!("{:?}", Rc::strong_count(&truck1)); // prints 2. still will be 2 if u took strong count of &truck2
1923-
// // Ownership is transfered still No. of ref remains same - 2
1923+
// // Ownership is transferred still No. of ref remains same - 2
19241924
// let truck3 = truck1;
19251925
// // No. of ref is still 2. still will be 2 if u took strong count of &truck3.
19261926
// println!("{:?}", Rc::strong_count(&truck2)); // prints 2
@@ -2047,4 +2047,190 @@
20472047

20482048
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
20492049

2050+
// 50. Custom Option methods impl
2051+
// #[allow(dead_code)]
2052+
// #[derive(Debug, PartialEq)]
2053+
// enum MyOption<T> {
2054+
// Some(T),
2055+
// None,
2056+
// }
2057+
2058+
// use MyOption::{None, Some};
2059+
2060+
// #[allow(dead_code)]
2061+
// impl<T> MyOption<T> {
2062+
// fn is_some(&self) -> bool {
2063+
// matches!(self, Some(_))
2064+
// }
2065+
2066+
// fn is_some_and<F>(self, func: F) -> bool
2067+
// where
2068+
// F: FnOnce(T) -> bool,
2069+
// {
2070+
// match self {
2071+
// Some(inner) => func(inner),
2072+
// None => false,
2073+
// }
2074+
// }
2075+
2076+
// fn as_ref(&self) -> MyOption<&T> {
2077+
// match self {
2078+
// Some(inner) => Some(inner),
2079+
// None => None,
2080+
// }
2081+
// }
2082+
2083+
// fn unwrap_or(self, default: T) -> T {
2084+
// match self {
2085+
// Some(x) => x,
2086+
// None => default,
2087+
// }
2088+
// }
2089+
2090+
// fn unwrap_or_else<F>(self, func: F) -> T
2091+
// where
2092+
// F: FnOnce() -> T,
2093+
// {
2094+
// match self {
2095+
// Some(x) => x,
2096+
// None => func(),
2097+
// }
2098+
// }
2099+
2100+
// fn unwrap_or_default(self) -> T
2101+
// where
2102+
// T: Default,
2103+
// {
2104+
// match self {
2105+
// Some(x) => x,
2106+
// None => T::default(),
2107+
// }
2108+
// }
2109+
2110+
// fn map<U, F>(self, func: F) -> MyOption<U>
2111+
// where
2112+
// F: FnOnce(T) -> U,
2113+
// {
2114+
// match self {
2115+
// Some(x) => Some(func(x)),
2116+
// None => None,
2117+
// }
2118+
// }
2119+
2120+
// fn inspect<F>(self, func: F) -> Self
2121+
// where
2122+
// F: FnOnce(&T),
2123+
// {
2124+
// if let Some(ref x) = self {
2125+
// func(x);
2126+
// }
2127+
2128+
// self
2129+
// }
2130+
2131+
// fn map_or<U, F>(self, default: U, func: F) -> U
2132+
// where
2133+
// F: FnOnce(T) -> U,
2134+
// {
2135+
// match self {
2136+
// Some(x) => func(x),
2137+
// None => default,
2138+
// }
2139+
// }
2140+
2141+
// fn map_or_else<U, F, G>(self, default: G, func: F) -> U
2142+
// where
2143+
// F: FnOnce(T) -> U,
2144+
// G: FnOnce() -> U,
2145+
// {
2146+
// match self {
2147+
// Some(x) => func(x),
2148+
// None => default(),
2149+
// }
2150+
// }
2151+
2152+
// fn ok_or<E>(self, err: E) -> Result<T, E> {
2153+
// match self {
2154+
// Some(x) => Ok(x),
2155+
// None => Err(err),
2156+
// }
2157+
// }
2158+
2159+
// fn ok_or_else<F, E>(self, func: F) -> Result<T, E>
2160+
// where
2161+
// F: FnOnce() -> E,
2162+
// {
2163+
// match self {
2164+
// Some(x) => Ok(x),
2165+
// None => Err(func()),
2166+
// }
2167+
// }
2168+
2169+
// fn and<U>(self, next: MyOption<U>) -> MyOption<U> {
2170+
// match self {
2171+
// Some(_) => next,
2172+
// None => None,
2173+
// }
2174+
// }
2175+
2176+
// fn and_then<U, F>(self, next_func: F) -> MyOption<U>
2177+
// where
2178+
// F: FnOnce(T) -> MyOption<U>,
2179+
// {
2180+
// match self {
2181+
// Some(x) => next_func(x),
2182+
// None => None,
2183+
// }
2184+
// }
2185+
2186+
// fn filter<P>(self, predicate: P) -> Self
2187+
// where
2188+
// P: FnOnce(&T) -> bool,
2189+
// {
2190+
// if let Some(x) = self {
2191+
// if predicate(&x) {
2192+
// return Some(x);
2193+
// }
2194+
// }
2195+
// None
2196+
// }
2197+
2198+
// fn or(self, default: Self) -> Self {
2199+
// match self {
2200+
// val @ Some(_) => val,
2201+
// None => default,
2202+
// }
2203+
// }
2204+
2205+
// fn or_else<F>(self, default: F) -> Self
2206+
// where
2207+
// F: FnOnce() -> Self,
2208+
// {
2209+
// match self {
2210+
// val @ Some(_) => val,
2211+
// None => default(),
2212+
// }
2213+
// }
2214+
2215+
// fn xor(self, other: Self) -> Self {
2216+
// match (self, other) {
2217+
// (val @ Some(_), None) => val,
2218+
// (None, val @ Some(_)) => val,
2219+
// _ => None,
2220+
// }
2221+
// }
2222+
2223+
// fn take(self) -> Self {
2224+
// self
2225+
// }
2226+
// }
2227+
2228+
// fn main() {
2229+
// let x: MyOption<i32> = Some(5);
2230+
// let y = x.take();
2231+
// println!("{y:?}");
2232+
// }
2233+
2234+
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
2235+
20502236
fn main() {}

0 commit comments

Comments
 (0)