Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print the type of a variable in Rust?

Tags:

types

rust

I have the following:

let mut my_number = 32.90; 

How do I print the type of my_number?

Using type and type_of did not work. Is there another way I can print the number's type?

like image 397
user2431012 Avatar asked Feb 13 '14 06:02

user2431012


People also ask

How do you print a type of variable?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

What is type keyword in Rust?

The type keyword in rust has a different meaning in the 2 places it can be used: Type alias: Just another name for the same type. Associated types: this occurs within traits and trait impl blocks.

How do you print an array in Rust?

To print an array in Rust, we use the 😕 Operator inside the println! function.


2 Answers

You can use the std::any::type_name function. This doesn't need a nightly compiler or an external crate, and the results are quite correct:

fn print_type_of<T>(_: &T) {     println!("{}", std::any::type_name::<T>()) }  fn main() {     let s = "Hello";     let i = 42;      print_type_of(&s); // &str     print_type_of(&i); // i32     print_type_of(&main); // playground::main     print_type_of(&print_type_of::<i32>); // playground::print_type_of<i32>     print_type_of(&{ || "Hi!" }); // playground::main::{{closure}} } 

Be warned: as said in the documentation, this information must be used for a debug purpose only:

This is intended for diagnostic use. The exact contents and format of the string are not specified, other than being a best-effort description of the type.

If you want your type representation to stay the same between compiler versions, you should use a trait, like in the phicr's answer.

like image 171
Boiethios Avatar answered Oct 06 '22 08:10

Boiethios


If you merely wish to find out the type of a variable and are willing to do it at compile time, you can cause an error and get the compiler to pick it up.

For example, set the variable to a type which doesn't work:

let mut my_number: () = 32.90; // let () = x; would work too 
error[E0308]: mismatched types  --> src/main.rs:2:29   | 2 |     let mut my_number: () = 32.90;   |                             ^^^^^ expected (), found floating-point number   |   = note: expected type `()`              found type `{float}` 

Or call an invalid method:

let mut my_number = 32.90; my_number.what_is_this(); 
error[E0599]: no method named `what_is_this` found for type `{float}` in the current scope  --> src/main.rs:3:15   | 3 |     my_number.what_is_this();   |               ^^^^^^^^^^^^ 

Or access an invalid field:

let mut my_number = 32.90; my_number.what_is_this 
error[E0610]: `{float}` is a primitive type and therefore doesn't have fields  --> src/main.rs:3:15   | 3 |     my_number.what_is_this   |               ^^^^^^^^^^^^ 

These reveal the type, which in this case is actually not fully resolved. It’s called “floating-point variable” in the first example, and “{float}” in all three examples; this is a partially resolved type which could end up f32 or f64, depending on how you use it. “{float}” is not a legal type name, it’s a placeholder meaning “I’m not completely sure what this is”, but it is a floating-point number. In the case of floating-point variables, if you don't constrain it, it will default to f64¹. (An unqualified integer literal will default to i32.)

See also:

  • What is the {integer} or {float} in a compiler error message?

¹ There may still be ways of baffling the compiler so that it can’t decide between f32 and f64; I’m not sure. It used to be as simple as 32.90.eq(&32.90), but that treats both as f64 now and chugs along happily, so I don’t know.

like image 34
Chris Morgan Avatar answered Oct 06 '22 10:10

Chris Morgan