Should the self parameter be passed last?

⚓ Rust    📅 2025-08-29    👤 surdeus    👁️ 4      

surdeus

I'm writing a function that consumes self and accepts another parameter. This other parameter is a more complex struct that also contains data originating on self (copied or cloned). This doesn't work ergonomically, because the function parameters (including self) are passed in the wrong order by the language:

struct WorksOnSelf {
    uses_field: bool
}

impl WorksOnSelf {
    fn take_self_and_something_that_contains_own_field(self, field: bool) {}
}

fn example() {
    let work_on_self = WorksOnSelf { uses_field: true };

    // error: work_on_self is already moved in argument position
    work_on_self.take_self_and_something_that_contains_own_field(work_on_self.uses_field);
}

I don't see anything dictating that self must be moved into the function before the content of the parentheses is evaluated. I may be missing something. Is it conceivable that a future version of Rust may make this compile?

One way this could be done is to allow the self parameter to be placed into any position in the parameter list...

4 posts - 3 participants

Read full topic

🏷️ Rust_feed