Skip to content

Instantly share code, notes, and snippets.

@pnkfelix
Created March 25, 2013 16:36
Show Gist options
  • Save pnkfelix/5238517 to your computer and use it in GitHub Desktop.
Save pnkfelix/5238517 to your computer and use it in GitHub Desktop.
fn vec_peek<T>(v: &'r [T]) -> Option< (&'r T, &'r [T]) > {
if v.len() == 0 {
None
} else {
let head = &v[0];
let tail = v.view(1, v.len());
Some( (head, tail) )
}
}
@pnkfelix
Copy link
Author

/tmp/foo2.rs:6:19: 6:20 error: illegal borrow: borrowed value does not live long enough
/tmp/foo2.rs:6         let tail = v.view(1, v.len());
                                  ^
/tmp/foo2.rs:1:57: 9:1 note: borrowed pointer must be valid for the lifetime &'r  as defined on the block at 1:57...
/tmp/foo2.rs:1 fn vec_peek<T>(v: &'r [T]) -> Option< (&'r T, &'r [T]) > {
/tmp/foo2.rs:2     if v.len() == 0 {
/tmp/foo2.rs:3         None
/tmp/foo2.rs:4     } else {
/tmp/foo2.rs:5         let head = &v[0];
/tmp/foo2.rs:6         let tail = v.view(1, v.len());
               ...
/tmp/foo2.rs:1:0: 9:1 note: ...but borrowed value is only valid for the function body at 1:0
/tmp/foo2.rs:1 fn vec_peek<T>(v: &'r [T]) -> Option< (&'r T, &'r [T]) > {
/tmp/foo2.rs:2     if v.len() == 0 {
/tmp/foo2.rs:3         None
/tmp/foo2.rs:4     } else {
/tmp/foo2.rs:5         let head = &v[0];
/tmp/foo2.rs:6         let tail = v.view(1, v.len());
               ...
error: aborting due to previous error

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