Skip to content

Lifetime of reference taken for parameter vs lifetime of function resolution #16627

@serpent

Description

@serpent

In an example like

struct Foo
{
  n: u8
}

impl Foo {
  fn get(&self) -> u8 { self.n }
  fn set(&mut self, n: u8) { self.n = n }
}

fn main()
{
  let mut f = Foo { n : 0 };
  f.set(f.get() + 1);
}

one can't get and set in the same function call. (If the f.get() is placed in a temporary variable, then of course this works, but this is such a common idiom that I would think Rust should avoid requiring that).

I wonder if this could be improved? It seems like, if the borrow of the f.get() happens first, before the f.set, then it would be possible.

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