Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Calamari opened this issue Oct 27, 2014 · 1 comment
Closed

Comments

@Calamari
Copy link

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?

@Calamari
Copy link
Author

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

lnicola pushed a commit to lnicola/rust that referenced this issue Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant