Scope resolution newbie error

Hi my first post fellow Rustaceans.

I'm making some very basic error in syntax. I think it's because I split the code into two files, and It's something very simple since I can reach my impl functions but not the Traits.

Any help appreciated. Here is the source in two files: and

mod file1; //module defining Structure1 
use crate::file1::*;   
// use crate::file1::Structure1; //makes no difference below
fn main() {
    let my_s = Structure1 {number:10};

    println!("Structure1 number is: {0}", my_s.number);  //works fine: Structure1 number is: 10

    my_s.print_me(); //works fine: inside fn print_me
    file1::Structure1::print_me(&my_s); //works fine: "inside fn print_me" 

   //; //won't compile, gives error below


    help: items from traits can only be used if the trait is implemented and in scope
    note: `Thingy` defines an item `bar`, perhaps you need to implement it

Output (with above lines that don't compile commented out):

Structure1 number is: 10
inside fn print_me
inside fn print_me

pub struct Structure1 {
    pub number: i32

trait Thingy {
    fn bar(&self);

following format in which is different from:  links deleted 

impl Thingy for Structure1 {

    fn bar(&self){
        println!("inside fn bar");


impl Structure1 {
    pub fn print_me(&self){
    println!("inside fn print_me");   

Your formatting doesn't seem right. Could you enclose all your code snippets in ``` marks as described in Forum Code Formatting and Syntax Highlighting? This would make it much easier to read.

OK I'll try. it's in two different modules but I'll combine them. Thanks

Note that you can also edit the post.

Please format your code as per the pinned post, this is very hard to read.

Anyway, the requirement of bringing traits into scope is clearly documented in the relevant section of The Book.

Thanks. I tried again, the first post was hidden by the spam bot. This example uses two files, while The Book and most examples online put everything into which would work but is not my goal here. Any help appreciated. I code for fun, not a pro, not a student.

The Book's example I linked to doesn't put everything in one file. It demonstrates that in order to use the trait defined in from within, you have to add an explicit use Trait; declaration to This works with any other files or module/crate names.

1 Like

Thingy is private. Try

-    trait Thingy {
+    pub trait Thingy {

You get better errors without blob imports.

1 Like

Sorry for the inconvenience by the spam bot. I’ve restored this topic, fixed the formatting for you, and merged the replies from both threads ^^

1 Like

Thanks. I did with the three backticks but got: Our automated spam filter, Akismet, has temporarily hidden this topic. I'll see what I can do and might repost it.

Just to complete this topic, the sandbox solution did not work until I changed the trait Thingy to public: pub trait Thingy { fn bar(&self); }.

Further, the 'mod' keyword in is optional, and, as a matter of style, the import use file1::{Structure1,Thingy}; can be placed outside of at the top (or within, it's a matter of style).

I appreciate the feedback. I was about to actually abandon the Rust language if I could not get Traits to work, as they seem to be a useful thing to have, something like an interface in C#.

EDIT: in an ironic twist, it turns out the original code would also work by simply adding 'pub' in front of trait Thingy, as shown in the first line above. And the original "import" use lines in would also work (mod file1; use crate::file1::*; ) though I do like the more explicit form provided by the recommended answer here. What confused me before my first post here is that you cannot make a Trait 'public' in the actual implementation of the trait (i.e. at impl Thingy for Structure1) since that gives a compile error. And I maintain none of this is well documented in The Book. :slight_smile:

That kind of operation doesn't make sense though. A trait can only be defined once — that is the trait, and it's either public or not. But it can be implemented multiple times. But that would mean that some implementations could choose it to be public while others could choose it to be private. Obviously, the same trait can't be simultaneously public and private, so it isn't meaningful to let the impl blocks set the visibility of the trait.

1 Like

At a glance, the book does seem to concentrate on structs, not traits, in the relevant chapter. And I could see this error being improved to say "did you mean to mark the trait pub?".

But yeah, traits are implemented or not; privacy plays no direct role in whether a trait bound it met or not, for example.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.