summaryrefslogtreecommitdiff
path: root/improved_slice_patterns
diff options
context:
space:
mode:
authorNadrieril2019-04-14 23:05:52 +0200
committerNadrieril2019-04-14 23:05:52 +0200
commit6b60e0ded951faa6aff5091300307f5e4b139d8e (patch)
tree53f2e8045fe410b7d2bde8fc13cbb625b63ac0d4 /improved_slice_patterns
parent2954e502abb274b87ae3dbbb80ef523ebe602b28 (diff)
improved_slice_patterns: Improve docs
Diffstat (limited to 'improved_slice_patterns')
-rw-r--r--improved_slice_patterns/src/lib.rs21
1 files changed, 21 insertions, 0 deletions
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::<Vec<_>>()
+/// },
+/// [None, None] => {
+/// vec![]
+/// },
+/// );
+///
+/// assert_eq!(res, None); // there was no match
+///
/// # Ok::<(), ()>(())
/// ```
///