algonomicon
algonomicon

Reputation: 43

Why can't I declare a trait function with tuple parameter matching?

Why is it that I cannot declare a trait function with tuple parameter matching?

#![allow(unused)]

// This works
fn foo((x, y): (i32, i32)) {
}

trait Bar {
    // This does not work
    fn bar((x, y): (i32, i32));
}

Playground

Compiling the above outputs this:

error: expected one of `)` or `,`, found `:`
 --> src/main.rs:7:18
  |
7 |     fn bar((x, y): (i32, i32));
  |                  ^ expected one of `)` or `,` here

error: expected one of `!`, `&&`, `&`, `(`, `)`, `*`, `<`, `?`, `[`, `_`, `dyn`, `extern`, `fn`, `for`, `impl`, `unsafe`, or lifetime, found `:`
 --> src/main.rs:7:18
  |
7 |     fn bar((x, y): (i32, i32));
  |                  ^ expected one of 17 possible tokens here

error[E0601]: `main` function not found in crate `playground`
  |
  = note: consider adding a `main` function to `src/main.rs`

Upvotes: 4

Views: 371

Answers (1)

Peter Hall
Peter Hall

Reputation: 58735

This syntax is not supported in Rust, and there currently are no open RFCs to change that.

In a trait it would serve no purpose other than perhaps for documentation. But, since you are defining the trait anyway, you could just define a more descriptive type for that argument in the first place. In your case, a Point with x and y fields.

Upvotes: 2

Related Questions