Compilation error when trying to print an integer in Rust v0.13.0

I thought this would work:

let x = 5;
println!("x = {}", x);

      

But it gives the following compilation error:

main.rs:3:24: 3:25 error: unable to infer enough type information to locate the impl of the trait `core::fmt::Show` for the type `_`; type annotations required                                                                             
main.rs:3     println!("x = {}", x);

      

Am I missing something?

I am using the online Rust compiler and their version is Rust v0.13.0

.

+3


source to share


1 answer


The error is that the compiler you are using is old. For this compiler, try explicitly specifying the integer type:

let x: i32 = 5;
println!("x = {}", x);

      

In newer compilers, your code will work as is, even without i32

explicitly specifying :



let x = 5;
println!("x = {}", x);

      

You can use the official online compiler at https://play.rust-lang.org/ , which is always an updated version of Rust.

+10


source







All Articles