Skip to content

Should the mutable borrow of self in method call range over the arguments? #20403

@honzasp

Description

@honzasp

When a &mut self method is called, the receiver is mutably borrowed before the arguments are evaluated, so the following program does not compile:

fn main() {
  let mut vec = vec![1, 2, 3];
  vec.push(vec.len());
  assert_eq!(vec, vec![1, 2, 3, 4]);
}

But when the argument explicitly evaluated before the call in a let statement, is compiles just fine:

fn main() {
  let mut vec = vec![1, 2, 3];
  let vec_len = vec.len();
  vec.push(vec_len);
  assert_eq!(vec, vec![1, 2, 3, 3]);
}

I am not sure if there is a good reason for the first program to be rejected, I would expect e1(e2) to be equivalent to { let x = e2; e1(x) }.

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