Find the type of a variable in Rust

I was trying to make an abstraction in Rust and found myself navigating a lot of code to determine the type of pwm below so I could put it in a struct:

let mut pwm = Timer::tim2(dp.TIM2, &clocks, &mut rcc.apb1)
    .pwm::<Tim2NoRemap, _, _, _>(c1, &mut afio.mapr, 1.khz());

A quicker way to find it is to add an empty type, : () and check the compilation error:

let mut pwm: () = Timer::tim2(dp.TIM2, &clocks, &mut rcc.apb1)
    .pwm::<Tim2NoRemap, _, _, _>(c1, &mut afio.mapr, 1.khz());

I’m using Visual Studio Code with the Rust Analyzer plugin and there I immediately get the error and can copy the type by hovering on the line:

So there you have it, the type was as simple as this – no wonder I gave up on trying to find it in the code! 😅

type PwmType = hal::pwm::Pwm<
    hal::pac::TIM2,
    hal::timer::Tim2NoRemap,
    hal::pwm::C1,
    hal::gpio::gpioa::PA0<hal::gpio::Alternate<hal::gpio::PushPull>>,
>;

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a robot? * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.