Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile 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 compile 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'm using an online Rust compiler and the version they have is Rust v0.13.0.

like image 536
sashoalm Avatar asked May 11 '15 05:05

sashoalm


1 Answers

The error is because the compiler you use is old. For this compiler, try by explicitly giving an integer type:

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

On newer compilers, your code will work as is, even without specifying i32 explicitly:

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

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

like image 74
gustafbstrom Avatar answered Oct 16 '22 19:10

gustafbstrom