diff --git a/src/re_unicode.rs b/src/re_unicode.rs index d90be4fb1c..d952461d51 100644 --- a/src/re_unicode.rs +++ b/src/re_unicode.rs @@ -73,6 +73,12 @@ impl<'t> Match<'t> { } } +impl<'t> From> for &'t str { + fn from(m: Match<'t>) -> &'t str { + m.as_str() + } +} + /// A compiled regular expression for matching Unicode strings. /// /// It is represented as either a sequence of bytecode instructions (dynamic) diff --git a/tests/api_str.rs b/tests/api_str.rs index 5bdca8426a..410bd9036a 100644 --- a/tests/api_str.rs +++ b/tests/api_str.rs @@ -20,3 +20,12 @@ fn empty_match_unicode_captures_iter() { .collect(); assert_eq!(vec![(0, 0), (3, 3), (4, 4), (7, 7), (8, 8)], ms); } + +#[test] +fn match_as_str() { + let re = regex!(r"fo+"); + let caps = re.captures("barfoobar").unwrap(); + assert_eq!(caps.get(0).map(|m| m.as_str()), Some("foo")); + assert_eq!(caps.get(0).map(From::from), Some("foo")); + assert_eq!(caps.get(0).map(Into::into), Some("foo")); +}