|
26 | 26 | //! |
27 | 27 | //! ## Implicit Mapping |
28 | 28 | //! |
29 | | -//! `=>` and the part that comes after can be omitted (requires `implicit_map` |
| 29 | +//! `=>` and the following part can be omitted (requires `implicit_map` |
30 | 30 | //! feature, which is enabled by default; you can disable it to skip the |
31 | 31 | //! compilation of the internal procedural macro): |
32 | 32 | //! |
|
199 | 199 | //! # let array = [Some(UncopyValue), None]; |
200 | 200 | //! let _: &UncopyValue = try_match!(&array[0], Some(x)).unwrap(); |
201 | 201 | //! ``` |
202 | | -//! |
203 | | -//! # Applications |
204 | | -//! |
205 | | -//! ## `Iterator::filter_map` |
206 | | -//! |
207 | | -//! ```rust |
208 | | -//! # use try_match::try_match; |
209 | | -//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 } |
210 | | -//! # use Enum::{Var1, Var2}; |
211 | | -//! let array = [Var1(42), Var2, Var1(10)]; |
212 | | -//! let filtered: Vec<_> = array |
213 | | -//! .iter() |
214 | | -//! .filter_map(|x| try_match!(x, &Var1(_0) if _0 > 20).ok()) |
215 | | -//! .collect(); |
216 | | -//! assert_eq!(filtered, [42]); |
217 | | -//! ``` |
218 | | -//! |
219 | | -//! ## `Iterator::map` + Fallible `Iterator::collect` |
220 | | -//! |
221 | | -//! ```rust |
222 | | -//! # use try_match::try_match; |
223 | | -//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 } |
224 | | -//! # use Enum::{Var1, Var2}; |
225 | | -//! let array = [Var1(42), Var2, Var1(10)]; |
226 | | -//! let filtered: Result<Vec<_>, _> = array |
227 | | -//! .iter() |
228 | | -//! .map(|x| try_match!(x, &Var1(_0) if _0 > 20)) |
229 | | -//! .collect(); |
230 | | -//! |
231 | | -//! // `Var2` is the first value that doesn't match |
232 | | -//! assert_eq!(filtered, Err(&Var2)); |
233 | | -//! ``` |
234 | | -//! |
235 | | -//! ## Extract Variants |
236 | | -//! |
237 | | -//! ```rust |
238 | | -//! # use try_match::try_match; |
239 | | -//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 } |
240 | | -//! # use Enum::{Var1, Var2}; |
241 | | -//! impl<T> Enum<T> { |
242 | | -//! fn var1(&self) -> Option<&T> { |
243 | | -//! try_match!(self, Var1(_0)).ok() |
244 | | -//! } |
245 | | -//! |
246 | | -//! fn is_var2(&self) -> bool { |
247 | | -//! matches!(self, Var2) |
248 | | -//! } |
249 | | -//! } |
250 | | -//! |
251 | | -//! let enums = [Var1(42), Var2]; |
252 | | -//! assert_eq!(enums[0].var1(), Some(&42)); |
253 | | -//! assert_eq!(enums[1].var1(), None); |
254 | | -//! |
255 | | -//! assert!(!enums[0].is_var2()); |
256 | | -//! assert!(enums[1].is_var2()); |
257 | | -//! ``` |
258 | | -//! |
259 | | -//! ## Expect Certain Variants |
260 | | -//! |
261 | | -//! ```rust |
262 | | -//! # use try_match::try_match; |
263 | | -//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 } |
264 | | -//! # use Enum::{Var1, Var2}; |
265 | | -//! fn this_fn_expects_var1(foo: &Enum<[u8; 4]>) { |
266 | | -//! let (i0, i1) = try_match!(foo, &Var1([_0, _, _, _1])).unwrap(); |
267 | | -//! |
268 | | -//! // Once RFC 1303 is stabilized, you can do instead: |
269 | | -//! // let &Var1([i0, _, _, i1]) = foo else { panic!("{:?}", foo) }; |
270 | | -//! |
271 | | -//! assert_eq!((i0, i1), (42, 45)); |
272 | | -//! } |
273 | | -//! |
274 | | -//! this_fn_expects_var1(&Var1([42, 43, 44, 45])); |
275 | | -//! ``` |
276 | | -//! |
277 | | -//! # Related Work |
278 | | -//! |
279 | | -//! [`matcher::matches!`][] (now incorporated into the standard library as |
280 | | -//! [`core::matches!`][]) is similar but only returns `bool` indicating whether |
281 | | -//! matching was successful or not. |
282 | | -//! |
283 | | -//! ``` |
284 | | -//! # use try_match::try_match; |
285 | | -//! let success1 = matches!(Some(42), Some(_)); |
286 | | -//! let success2 = try_match!(Some(42), Some(_)).is_ok(); |
287 | | -//! assert_eq!(success1, success2); |
288 | | -//! ``` |
289 | | -//! |
290 | | -//! [`bind_match::bind_match!`][] and [`extract::extract!`][] use the same |
291 | | -//! syntax (except for implicit mapping) but return `Some(expr)` on success |
292 | | -//! instead. |
293 | | -//! |
294 | | -//! [`core::matches!`]: https://doc.rust-lang.org/1.56.0/core/macro.matches.html |
295 | | -//! [`matcher::matches!`]: https://crates.io/crates/matches |
296 | | -//! [`bind_match::bind_match!`]: https://crates.io/crates/bind_match |
297 | | -//! [`extract::extract!`]: https://crates.io/crates/extract_macro |
298 | | -//! |
299 | 202 | #![no_std] |
300 | 203 | #![forbid(unsafe_code)] |
301 | 204 |
|
|
0 commit comments