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

Order of execution and borrow checker #61245

Closed
muyiliqing opened this issue May 27, 2019 · 2 comments
Closed

Order of execution and borrow checker #61245

muyiliqing opened this issue May 27, 2019 · 2 comments

Comments

@muyiliqing
Copy link

The following code gets an error from compiler:

struct VecHolder {
    vec: Vec<i32>,
}

impl VecHolder {
    pub fn run(self) -> Self {
        self.inner_run(self.vec.clone())
    }

    fn inner_run(self, _vec: Vec<i32>) -> Self {
        self
    }
}

Error message is

error[E0382]: use of moved value: `self.vec`
 --> test2.rs:7:24
  |
7 |         self.inner_run(self.vec.clone())
  |         ----           ^^^^^^^^ value used here after move
  |         |
  |         value moved here
  |
  = note: move occurs because `self` has type `VecHolder`, which does not implement the `Copy` trait

But if run is like the following, the code compiles:

    pub fn run(self) -> Self {
        let tmp = self.vec.clone();
        self.inner_run(tmp)
    }

The two versions are just equivalent to me. Is this a bug or a feature?

I found related discussion in rust internals and github. Not sure if any documentation is produced.

@muyiliqing muyiliqing changed the title Order of execusion and borrow checker Order of execution and borrow checker May 27, 2019
@jonas-schievink
Copy link
Contributor

This doesn't really have to do with the borrow checker, in function calls the receiver is just evaluated before any arguments. This is intentional (it's basically an implication of "function arguments are evaluated left-to-right"), and can't be changed because unsafe code can rely on it.

@pnkfelix
Copy link
Member

Yep, this is not a bug. Things are running as designed. Closing as not-a-bug.

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

3 participants