Skip to content

Document how use path::Trait; pulls impls into scope #11690

@seanmonstar

Description

@seanmonstar

Our documentation on this matter is pretty poor; we need at a minimum to:

  • Document this particular case of use declaration
  • Write a small example pulling a trait from another mod
  • Put cross-references to the new section as appropriate (from the sections on use and on Traits, at a minimum).

Original bug report follows


Original title: "error "does not implement any method in scope named" if trait is not imported into mod"

I have a struct that implements a trait, and then an other module (the test module) that uses my struct. I get a compiler error when calling a method from that trait, unless I also use the trait.

Test Code:

mod foo {
  pub trait Foo {
    fn foo(&self) {

    }
  }
}

mod bar {
  use foo::Foo;

  pub struct Bar;
  impl Foo for Bar {
    fn foo(&self) {
      println!("foobar!");
    }
  }

  #[cfg(test)]
  mod tests {
    use super::Bar;
    //use foo::Foo; // uncomment this means no error

    #[test]
    fn foobar() {
      let bar = Bar;
      bar.foo();
    }
  }
}

Ouput of rustc -v:

D:\Program Files (x86)\Rust\bin\rustc.exe 0.9 (7613b15 2014-01-08 18:04:43 -0800)
host: i686-pc-mingw32

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

    Issue actions