Skip to content

Instantly share code, notes, and snippets.

@lilyball
Last active December 17, 2015 20:29
Show Gist options
  • Save lilyball/5668099 to your computer and use it in GitHub Desktop.
Save lilyball/5668099 to your computer and use it in GitHub Desktop.
// ...
pub trait VecUtil<Elem> {
pub fn grouped(&self, n: uint) -> ~[&[Elem]];
}
impl<'self, Elem> VecUtil<Elem> for &'self [Elem] {
fn grouped(&self, n: uint) -> ~[&'self [Elem]] {
if self.is_empty() { return ~[]; }
let mut res = vec::with_capacity((self.len()-1) / n + 1);
for uint::range_step(0, self.len(), n) |i| {
res.push(self.slice(i, i+n));
}
res
}
}
@lilyball
Copy link
Author

util.rs:87:1: 94:2 error: method `grouped` has an incompatible type: lifetime mismatch
util.rs:87  fn grouped(&self, n: uint) -> ~[&'self [Elem]] {
util.rs:88      if self.is_empty() { return ~[]; }
util.rs:89      let mut res = vec::with_capacity((self.len()-1) / n + 1);
util.rs:90      for uint::range_step(0, self.len(), n) |i| {
util.rs:91          res.push(self.slice(i, i+n));
util.rs:92      }
           ...
note: lifetime re_infer(ReSkolemized(1, br_anon(1)))...
util.rs:87:48: 94:2 note: ...does not necessarily outlive the lifetime &'self  as defined on the block at 87:48
util.rs:87  fn grouped(&self, n: uint) -> ~[&'self [Elem]] {
util.rs:88      if self.is_empty() { return ~[]; }
util.rs:89      let mut res = vec::with_capacity((self.len()-1) / n + 1);
util.rs:90      for uint::range_step(0, self.len(), n) |i| {
util.rs:91          res.push(self.slice(i, i+n));
util.rs:92      }
           ...
error: aborting due to previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment