Skip to content

Incorrect loan error when local borrowed variable is updated to new loan #5550

Closed
@lifthrasiir

Description

@lifthrasiir

The following code...

fn main() {
    let s: ~str = ~"foobar";
    let mut t: &str = s;
    t = t.slice(0, 3); // for master: str::view(t, 0, 3) maybe
}

...results in the following error:

t.rs:4:4: 4:5 error: assigning to mutable local variable prohibited due to outstanding loan
t.rs:4     t = t.slice(0, 3);
           ^
t.rs:4:8: 4:9 note: loan of mutable local variable granted here
t.rs:4     t = t.slice(0, 3);
               ^

Both t and t.slice(0, 3) are loans of s, so it should be safe to update t to new loan of s as updating t doesn't cause s to be freed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions