Skip to content

Instantly share code, notes, and snippets.

@ben0x539
Last active December 14, 2015 07:29
Show Gist options
  • Save ben0x539/5051018 to your computer and use it in GitHub Desktop.
Save ben0x539/5051018 to your computer and use it in GitHub Desktop.
struct Vec<'self, T> {
vec: &'self [T]
}
fn advance<'lt, T>(w: &mut Vec<'lt, T>) {
w.vec = vec::slice(w.vec, 1, w.vec.len());
}
fn wrap<'lt, T>(v: &'lt [T]) -> Vec<'lt, T> {
Vec { vec: v }
}
fn main() {
let v = [1, 2, 3, 4];
let mut w = wrap(v);
while w.vec.len() > 0 {
io::println(fmt!("%d", w.vec[0]));
advance(&mut w);
}
}
foo.rs:10:15: 10:16 error: illegal borrow: borrowed value does not live long enough
foo.rs:10 Vec { vec: v }
^
foo.rs:9:44: 11:1 note: borrowed pointer must be valid for the anonymous lifetime #1 defined on the block at 9:44...
foo.rs:9 fn wrap<'lt, T>(v: &'lt [T]) -> Vec<'lt, T> {
foo.rs:10 Vec { vec: v }
foo.rs:11 }
foo.rs:9:44: 11:1 note: ...but borrowed value is only valid for the lifetime &lt as defined on the block at 9:44
foo.rs:9 fn wrap<'lt, T>(v: &'lt [T]) -> Vec<'lt, T> {
foo.rs:10 Vec { vec: v }
foo.rs:11 }
foo.rs:16:10: 16:11 error: loan of mutable local variable as immutable conflicts with prior loan
foo.rs:16 while w.vec.len() > 0 {
^
foo.rs:18:21: 18:22 note: prior loan as mutable granted here
foo.rs:18 advance(&mut w);
^
error: aborting due to 2 previous errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment