Consider this code:
use chrono::{Local, NaiveDate};
fn main() {
let d = Local::now().naive_local().date();
println!("{}", d.num_days_from_ce());
}
This fails to compile with the strange error, "no method named num_days_from_ce found for struct NaiveDate in the current scope".
What? No method name? It's right there in the documentation‽
It took me a while to figure out I needed to add Datelike
to the things I'm using from chrono. So my question is, why? Why force the consumers to have to import dependencies manually that we aren't using directly? This is not the only case of these necessary phantom imports in Rust...
Maybe there is something about Rust that I am not understanding here, or maybe there is something that can be changed about this library so that consumers don't need to use DateLike in this situation.
Also, why can't the compiler recommend WHAT to bring into the current scope?
Another issue with this syntax is that it produces a compiler warning that these necessary phantom imports are unused. Thus, there is no possible way for my code to compile cleanly...
warning: unused imports: `Datelike`, `NaiveDate`, `Weekday`
--> src/bin/foo/bar.rs:25:18
|
25 | use chrono::{Datelike, Duration, Local, NaiveDate, Weekday};
| ^^^^^^^^ ^^^^^^^^^ ^^^^^^^