Checked trait alternatives

This trait has two alternatives of implementing the same functionality. You should implement either get_x and get_y or get_coords. Is there any way to catch this at compile time? current it compiles and runs, but it will just crash with a stack overflow.

#[derive(Debug, Default)]
struct Point {
    x: i32,
    y: i32,

trait Test {
    fn get_x(&self) -> i32 {

    fn get_y(&self) -> i32 {

    fn get_coords(&self) -> Point {
        Point {
            x: self.get_x(),
            y: self.get_y(),

// bad implementation, this should be a compile error
impl Test for Point {}

fn main() {
    let a = Point::default();
    println!("{:?}", a.get_coords());

No, that's not possible. The idiomatic solution is to leave one of them unimplemented.

1 Like

There's nothing that exists today, though the problem has been discussed on Internals before:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.