|
2231 | 2231 | // println!("{y:?}"); |
2232 | 2232 | // } |
2233 | 2233 |
|
2234 | | -/////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2234 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2235 | + |
| 2236 | +// #[allow(dead_code)] |
| 2237 | +// #[derive(Debug)] |
| 2238 | +// pub enum MyResult<T, E> { |
| 2239 | +// Ok(T), |
| 2240 | +// Err(E), |
| 2241 | +// } |
| 2242 | + |
| 2243 | +// #[allow(dead_code)] |
| 2244 | +// use MyResult::{Err, Ok}; |
| 2245 | + |
| 2246 | +// #[allow(dead_code)] |
| 2247 | +// impl<T, E> MyResult<T, E> { |
| 2248 | +// fn is_ok(&self) -> bool { |
| 2249 | +// matches!(self, Ok(_)) |
| 2250 | +// } |
| 2251 | + |
| 2252 | +// fn is_ok_and<F>(self, func: F) -> bool |
| 2253 | +// where |
| 2254 | +// F: FnOnce(T) -> bool, |
| 2255 | +// { |
| 2256 | +// match self { |
| 2257 | +// Ok(x) => func(x), |
| 2258 | +// Err(_) => false, |
| 2259 | +// } |
| 2260 | +// } |
| 2261 | + |
| 2262 | +// fn is_err_and<F>(self, func: F) -> bool |
| 2263 | +// where |
| 2264 | +// F: FnOnce(E) -> bool, |
| 2265 | +// { |
| 2266 | +// match self { |
| 2267 | +// Ok(_) => false, |
| 2268 | +// Err(e) => func(e), |
| 2269 | +// } |
| 2270 | +// } |
| 2271 | +// fn ok(self) -> Option<T> { |
| 2272 | +// match self { |
| 2273 | +// Ok(x) => Some(x), |
| 2274 | +// Err(_) => None, |
| 2275 | +// } |
| 2276 | +// } |
| 2277 | + |
| 2278 | +// fn as_ref(&self) -> MyResult<&T, &E> { |
| 2279 | +// match *self { |
| 2280 | +// Ok(ref x) => Ok(x), |
| 2281 | +// Err(ref e) => Err(e), |
| 2282 | +// } |
| 2283 | +// } |
| 2284 | + |
| 2285 | +// fn map<F, U>(self, func: F) -> MyResult<U, E> |
| 2286 | +// where |
| 2287 | +// F: FnOnce(T) -> U, |
| 2288 | +// { |
| 2289 | +// match self { |
| 2290 | +// Ok(x) => Ok(func(x)), |
| 2291 | +// Err(e) => Err(e), |
| 2292 | +// } |
| 2293 | +// } |
| 2294 | + |
| 2295 | +// fn and_then<F, U>(self, func: F) -> MyResult<U, E> |
| 2296 | +// where |
| 2297 | +// F: FnOnce(T) -> MyResult<U, E>, |
| 2298 | +// { |
| 2299 | +// match self { |
| 2300 | +// Ok(x) => func(x), |
| 2301 | +// Err(e) => Err(e), |
| 2302 | +// } |
| 2303 | +// } |
| 2304 | +// } |
| 2305 | + |
| 2306 | +// fn main() { |
| 2307 | +// let res: MyResult<i32, i32> = Ok(5); |
| 2308 | + |
| 2309 | +// let x = res.and_then(|inner| Ok(inner + 1)); |
| 2310 | + |
| 2311 | +// println!("{x:?}"); |
| 2312 | +// } |
| 2313 | + |
| 2314 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
2235 | 2315 |
|
2236 | 2316 | fn main() {} |
0 commit comments