[Solved]Question about the drop() fuction and borrows


There is an example at https://doc.rust-lang.org/std/mem/fn.drop.html

fn main() {
    let mut v = vec![1, 2, 3];
let x = &v[0];

drop(x); // explicitly drop the reference, but the borrow still exists

v.push(4); // error: cannot borrow `v` as mutable because it is also
           // borrowed as immutable

I don’t understand why the x is explicitly droped,but the borrow still exists.
(Though,there is an explaination:borrows are based on lexical scope.I’m not good at english,what is a “lexical scope”?)

By the way, what is the sign “~” means at a struct field?

pub struct GameManager<'self>{
    lvl: Level,
    actors: ~[Actor<'self>]



It’s all good, this isn’t so much an “English” thing as it is a “computer science” thing. See https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping_vs.dynamic_scoping and https://en.wikipedia.org/wiki/Scope(computer_science)#Lexical_scoping

Put another way, the answer to your question is, borrows are not based on when something is dropped. This behavior is planned to change in the future.

Invalid, very old syntax. This would be actors: Vec<Actor> today.


Thank you! Does it means that once a borrow is created in a scope,it will alway be valid(can not be deleted)?


Yes, the only way to end a borrow is to end a scope. So in this case, you’d need

fn main() {
    let mut v = vec![1, 2, 3];

        let x = &v[0];

    }  // scope is over, x goes away, so does the borrow

    v.push(4); // now this works