Replace type annotation with turbofish

Hi,

Until this day I used to think that one can always substitute one for another. The annotation is good:

use chrono::{DateTime, Utc};
    ...
    let stamp: DateTime<Utc> = p.metadata().unwrap().created().unwrap().into();

an attempt at turbofishing

    let stamp = p.metadata().unwrap().created().unwrap().into::<DateTime<Utc>>();

results in error:

919 |          let stamp = p.metadata().unwrap().created().unwrap().into::<DateTime<Utc>>();
    |                                                               ^^^^----------------- help: remove these generics
    |                                                               |
    |                                                               expected 0 generic arguments

It's either turbofish can't always make it, or I'm putting the wrong fish in the wrong place.

Because Into::into has no type argument, you cannot turbofish (is turbofishing a verb?) that method. But Into has a type argument, so you can do:

fn main() {
    let _i: i32 = 5;
    let _big = Into::<i64>::into(_i);
}

(Playground)

P.S.: Idiomatically, I'd rather write: i64::from(_i) (without turbofish).

4 Likes

FYI, I found that limitation annoying, so here's a workaround :wink:

3 Likes

Thank you very much!

DateTime::<Utc>::from(date)

works.

The English verbs are a standing army, always relying heavily on conscription.

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.