Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Last active December 19, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimundi/5943107 to your computer and use it in GitHub Desktop.
Save Kimundi/5943107 to your computer and use it in GitHub Desktop.

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 ...
@auroranockert
Copy link

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()?

@hatahet
Copy link

hatahet commented Jul 9, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment