See a Value’s Type in Rust — Quick and Dirty

Joe Kreydt
Oct 26, 2019 · 2 min read

When working with a strongly typed language, like Rust, you are bound to run into compiler errors that may or may not be type-related. The Rust compiler is usually good about telling the user what type it found and what type it expected, but there are times when it doesn’t.

Here’s a quick and dirty hack for those times when you need to see the data type of a value in a Rust program.

  1. Declare a unit/nil type variable — that’s just parentheses. If you’re interested, here’s some documentation on Rust’s unit type.
  2. Assign the variable whose type you want to check to the unit variable.
  3. Run compile the program — rustc or cargo run.

The compiler will throw an error that indicates the variable’s type.

Here’s an example

fn main() {    let my_var: u32 = 122500;    let () = my_var;}

Running that program in a terminal looks like this

The red text that says “^^ expected…” is the key.

You can translate Rust telling you what type it expected as Rust telling you the type of the variable you assigned to ().

That really is quick and dirty, eh? It’s the easiest way I have found to see a value’s type in Rust. I hope it helps you as much as it has helped me!

Do you know of or use any other methods for finding the type of a variable in Rust? Let us know!

Joe Kreydt

Written by

Christ follower, systems analyst, idea gardener, software developer.

More From Medium

More from Joe Kreydt

Also tagged Programming Tips

Also tagged Programming Tips

Learning C++: Lambdas and the STL

Also tagged Programming Tips

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade