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

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

Closed
serpent opened this issue Aug 20, 2014 · 1 comment
Closed

Comments

@serpent
Copy link

serpent commented Aug 20, 2014

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.

@pcwalton
Copy link
Contributor

Dupe of #6268

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 3, 2024
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