public
Last active

  • Download Gist
AssignmentStatement.md
Markdown

Proposal for an additional use case of the in keyword besides for-loops:

Right now you can destructure with a pattern everywhere where you can introduce new local variables: let, match and in function signatures.

However, sometimes you are in the need of destructuring some values, but don't want to introduce a new variable for them, instead intending to store their result in some mutable bindings that are already in scope. To achieve that right now, you have to create a temporary local variable and move its content to the other one. Example:

fn foo() -> (int, int, int);

let mut x = 42;
let mut z = 0;
loop {
    let (x_, _, z_) = foo();
    x = x_;
    y = y_;
    // ...
}
// Do something with x, y ...

If we gain the in keyword, it could be used for a new assignment statement that works similar to let, but doesn't introduce new variables:

fn foo() -> (int, int, int);

let mut x = 42;
let mut z = 0;
loop {
    in (x, _, z) = foo();
    // ...
}
// Do something with x, y ...

Forgive me for not researching the background fully.

But is there a reason why the syntax couldn't just be (x, _, z) = foo(); or even better x, _, z = foo()?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.