What is the exact meaning of "visible" in Rust

In method call expression subclause, it says

Then, for each candidate type T, search for a visible method with a receiver of that type in the following places:

The term visible refers to Visibility and privacy - The Rust Reference, which basically says about access control specifier. However, consider this example

fn main(){
   let s = String::new();
   s.deref(); // #1
}

If we don't import Deref trait in the module, #1 will be an error. Is this about visible? If it is, the rust reference seems to not metione this part.

The second of “the following places” is:

  1. Any of the methods provided by a visible trait implemented by T. If T is a type parameter, methods provided by trait bounds on T are looked up first. Then all remaining methods in scope are looked up.

“In scope” isn't actually given a definition but I believe that's referring to the behavior you are asking about.

1 Like

So, what about inherent methods? There is nothing says the item should be in scope anyway.

Inherent methods are the first point:

  1. T's inherent methods (methods implemented directly on T).

But the rule didn't say the inherent item should be in scope.

That's because it doesn't have to - see this playground:

mod processor {
    pub struct Data(u64);
    pub fn process<F: FnOnce(Data)>(f: F) {
        f(Data(42))
    }
    impl Data {
        pub fn inner(&self) -> u64 {
            self.0
        }
    }
}

mod user {
    pub fn use_data() {
        // `Data` is not in scope here, since it's not `use`d
        // But we can call `Data::inner` anyway, since the compiler knows,
        // that the closure passed to `process` has its first argument of type `Data`
        super::processor::process(|data| println!("{}", data.inner()));
    }
}
1 Like

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.