From 6b60e0ded951faa6aff5091300307f5e4b139d8e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:05:52 +0200 Subject: improved_slice_patterns: Improve docs --- improved_slice_patterns/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 726b57a..56547f9 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,5 +1,10 @@ #![feature(slice_patterns)] +//! A tiny crate that provides two macros to help matching +//! on `Vec`s and iterators using [`slice_patterns`][slice_patterns] +//! +//! [slice_patterns]: https://doc.rust-lang.org/nightly/unstable-book/language-features/slice-patterns.html + /// Destructure an iterator using the syntax of slice_patterns. /// Wraps the match body in `Some` if there was a match; returns /// `None` otherwise. @@ -118,6 +123,8 @@ macro_rules! destructure_iter { /// Pattern-match on a vec using the syntax of slice_patterns. /// Wraps the match body in `Some` if there was a match; returns /// `None` otherwise. +/// Contrary to slice_patterns, this allows moving out +/// of the `Vec`. /// A variable length pattern (`x..`) returns an iterator. /// /// Example: @@ -139,6 +146,20 @@ macro_rules! destructure_iter { /// /// assert_eq!(res, Some(vec![Some(2), Some(3)])); /// +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = match_vec!(vec; +/// [Some(_), y.., Some(_)] => { +/// y.collect::>() +/// }, +/// [None, None] => { +/// vec![] +/// }, +/// ); +/// +/// assert_eq!(res, None); // there was no match +/// /// # Ok::<(), ()>(()) /// ``` /// -- cgit v1.2.3