Files
third_party_rust_rust-std-c…/README.md
T
2014-12-04 11:11:01 -08:00

1.5 KiB

These are candidates for addition to the Rust standard library. Each crate is published separately on crate.io

The matches! macro

RFC #163

A macro to evaluate, as a boolean, whether an expression matches a pattern.

#![feature(phase)]
#[phase(plugin)] extern crate matches;

fn is_slash(input: &str, pos: uint) -> bool {
    matches!(input.char_at(pos), '/' | '\\')
}
#![feature(phase)]
#[phase(plugin)] extern crate matches;
extern crate serialize;
use serialize::json::Json;

fn object_has_key(json: &Json, key: &str) -> bool {
    matches!(json, &Json::Object(ref obj) if obj.contains_key(key))
}

The zip_longest iterator adaptor

PR #19283

The standard library has an iterator.zip(other_iterator) method that returns a new iterator that yields pairs, and stops when one of the input iterators does.

zip_longest is similar, but instead continues until both iterators are exhausted. Instead of a pair of values (A, B), it yield pairs of optional values (Option<A>, Option<B>), where None indicates that one of the input iterator was exhausted before the other. ((None, None) never occurs.)

extern crate "zip-longest" as zip_longest;
use zip_longest::ZipLongestIteratorExt;

fn iter_eq<I, J, T>(i: I, j: J) -> bool
where I: Iterator<T>, J: Iterator<T>, T: Eq {
    i.zip_longest(j).all(|(a, b)| a == b)
}