What's the reason to bring trait into scope before using it?

error[E0599]: no method named `hour` found for type `chrono::naive::datetime::NaiveDateTime` in the current scope
  --> benches/my_benchmark.rs:55:32
   |
55 |             let minutes = (date.hour() * 60 + date.minute()) as u16;
   |                                ^^^^
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
   |
1  | use chrono::Timelike;

Why?

Because two different traits (which can come from two separate crates) can define hour method, so compiler will not be able to determine which implementation to use.

But I concur that the requirement to import traits often feels quite annoying and redundant, so I hope one day we will get "inherent traits", which will allow us to remove most of the trait imports in user code:
https://github.com/rust-lang/rfcs/pull/2375

2 Likes

Ah...I see...thanks!