Skip to content

type <MODULE>::<STRUCT> does not implement any method in scope named <METHOD> #18361

Closed
@Calamari

Description

@Calamari

Hello. I am not quite sure, if this is a bug, or I misunderstood something, but to me it looks wrong that I get the following error:

~/work/rusty/traits/src/main.rs:7:36: 7:43 error: type `traits::Point` does not implement any method in scope named `sub`
~/work/rusty/traits/src/main.rs:7   println!("p1-p2= x:{}, y:{}", p1.sub(p2).x, p1.sub(p2).y);
                                                                                   ^~~~~~~
~/work/rusty/traits/src/main.rs:7:50: 7:57 error: type `traits::Point` does not implement any method in scope named `sub`
~/work/rusty/traits/src/main.rs:7   println!("p1-p2= x:{}, y:{}", p1.sub(p2).x, p1.sub(p2).y);

Within this code:

fn main() {
  let p1 = traits::Point { x: 1, y: 1 };
  let p2 = traits::Point { x: 1, y: 1 };
  println!("p1+p2= x:{}, y:{}", p1.add(p2).x, p1.add(p2).y);
  println!("p1-p2= x:{}, y:{}", p1.sub(p2).x, p1.sub(p2).y);
}

mod traits {
  pub struct Point {
    pub x: int,
    pub y: int,
  }

  trait Subtract {
    pub fn sub(&self, p: Point) -> Point;
  }

  impl Point {
    pub fn add(&self, p: Point) -> Point {
      Point {
        x: self.x + p.x,
        y: self.y + p.y,
      }
    }
  }

  impl Subtract for Point {
    pub fn sub(&self, p: Point) -> Point {
      Point {
        x: self.x - p.x,
        y: self.y - p.y,
      }
    }
  }
}

While this code (same, but without the module definition) compiles and works perfectly:

//extern crate traits;

fn main() {
  let p1 = Point { x: 1, y: 1 };
  let p2 = Point { x: 1, y: 1 };
  println!("p1+p2= x:{}, y:{}", p1.add(p2).x, p1.add(p2).y);
  println!("p1-p2= x:{}, y:{}", p1.sub(p2).x, p1.sub(p2).y);
}

//mod traits {
  struct Point {
    x: int,
    y: int,
  }

  trait Subtract {
    fn sub(&self, p: Point) -> Point;
  }

  impl Point {
    fn add(&self, p: Point) -> Point {
      Point {
        x: self.x + p.x,
        y: self.y + p.y,
      }
    }
  }

  impl Subtract for Point {
    fn sub(&self, p: Point) -> Point {
      Point {
        x: self.x - p.x,
        y: self.y - p.y,
      }
    }
  }
//}

Is it a bug, or what am I doing wrong?

Activity

Calamari

Calamari commented on Oct 27, 2014

@Calamari
Author

Ah, Understood.
I missed a use traits::Subtract; statement.

added a commit that references this issue on Oct 22, 2024

Auto merge of rust-lang#18361 - Veykril:veykril/push-uzsokssoyznx, r=…

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Calamari

        Issue actions

          type `<MODULE>::<STRUCT>` does not implement any method in scope named `<METHOD>` · Issue #18361 · rust-lang/rust