Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.view(..) method has different borrow-check constraints than vec::slice(v, ..) #5541

Closed
pnkfelix opened this issue Mar 25, 2013 · 1 comment

Comments

@pnkfelix
Copy link
Member

This is spawned off from #3888.

If you swap in the first line below instead of the second, the example will compile.

fn vec_peek<T>(v: &'r [T]) -> &'r [T] {
    // let tail = vec::slice(v, 1, v.len());
    let tail = v.view(1, v.len());
    tail
}

Without the fix described above, you get the following compilation failure (rustc from commit c202430):

% rustc --lib /tmp/foo3.rs
/tmp/foo3.rs:3:15: 3:16 error: illegal borrow: borrowed value does not live long enough
/tmp/foo3.rs:3     let tail = v.view(1, v.len());
                              ^
/tmp/foo3.rs:1:38: 5:1 note: borrowed pointer must be valid for the lifetime &'r  as defined on the block at 1:38...
/tmp/foo3.rs:1 fn vec_peek<T>(v: &'r [T]) -> &'r [T] {
/tmp/foo3.rs:2     // let tail = vec::slice(v, 1, v.len());
/tmp/foo3.rs:3     let tail = v.view(1, v.len());
/tmp/foo3.rs:4     tail
/tmp/foo3.rs:5 }
/tmp/foo3.rs:1:0: 5:1 note: ...but borrowed value is only valid for the function body at 1:0
/tmp/foo3.rs:1 fn vec_peek<T>(v: &'r [T]) -> &'r [T] {
/tmp/foo3.rs:2     // let tail = vec::slice(v, 1, v.len());
/tmp/foo3.rs:3     let tail = v.view(1, v.len());
/tmp/foo3.rs:4     tail
/tmp/foo3.rs:5 }
error: aborting due to previous error
@lifthrasiir
Copy link
Contributor

Possibly related to #5550?

flip1995 pushed a commit to flip1995/rust that referenced this issue May 11, 2020
Extend example for the `unneeded_field_pattern` lint

Current example is incorrect (or pseudo-code) because a struct name is omitted. I have used the code from the tests instead. Perhaps this example can be made less verbose, but I think it is more convenient to see a "real" code as an example.

---

changelog: extend example for the `unneeded_field_pattern` lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants