Skip to content

Instantly share code, notes, and snippets.

@jld
Last active December 14, 2015 21:51
Show Gist options
  • Save jld/476a13a00ad05bd99a63 to your computer and use it in GitHub Desktop.
Save jld/476a13a00ad05bd99a63 to your computer and use it in GitHub Desktop.
In which borrowck errors are unhelpful.
src/main.rs:6:28: 6:29 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements [E0495]
src/main.rs:6 let caps = re.captures(s).unwrap();
^
src/main.rs:6:28: 6:29 note: first, the lifetime cannot outlive the expression at 6:27...
src/main.rs:6 let caps = re.captures(s).unwrap();
^
src/main.rs:6:28: 6:29 note: ...so that auto-reference is valid at the time of borrow
src/main.rs:6 let caps = re.captures(s).unwrap();
^
src/main.rs:6:16: 6:30 note: but, the lifetime must be valid for the method call at 6:15...
src/main.rs:6 let caps = re.captures(s).unwrap();
^~~~~~~~~~~~~~
src/main.rs:6:28: 6:29 note: ...so that argument is valid for the call
src/main.rs:6 let caps = re.captures(s).unwrap();
^
error: aborting due to previous error
extern crate regex;
use regex::Regex;
fn breaks(s: &str) -> usize {
let re = Regex::new(r"(?P<number>\d+)").unwrap();
let caps = re.captures(s).unwrap();
caps["number"].len()
}
fn works(s: &str) -> usize {
let re = Regex::new(r"(?P<number>\d+)").unwrap();
let caps = re.captures(s).unwrap();
caps.name("number").unwrap().len()
}
fn also_works(s: &str) -> usize {
let re = Regex::new(r"(?P<number>\d+)").unwrap();
let caps = re.captures(s).unwrap();
caps[1].len()
}
fn main() {
println!("{:?}", breaks("123"));
println!("{:?}", works("123"));
println!("{:?}", also_works("123"));
}
--- a/src/re.rs
+++ b/src/re.rs
@@ -970,11 +970,11 @@ impl<'t> Index<usize> for Captures<'t> {
///
/// # Panics
/// If there is no group named by the given value.
-impl<'t> Index<&'t str> for Captures<'t> {
+impl<'t, 'i> Index<&'i str> for Captures<'t> {
type Output = str;
- fn index<'a>(&'a self, name: &str) -> &'a str {
+ fn index<'a>(&'a self, name: &'i str) -> &'a str {
match self.name(name) {
None => panic!("no group named '{}'", name),
Some(ref s) => s,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment