Tag Archives: rust

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<