尝试在Rust v0.13.0中打印整数时编译错误

sas*_*alm 4 compiler-errors rust

我认为这会奏效:

let x = 5;
println!("x = {}", x);
Run Code Online (Sandbox Code Playgroud)

但它给出了以下编译错误:

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);
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

我正在使用在线Rust编译器,它们的版本是Rust v0.13.0.

gus*_*rom 10

该错误是因为您使用的编译器是旧的.对于此编译器,请尝试显式给出整数类型:

let x: i32 = 5;
println!("x = {}", x);
Run Code Online (Sandbox Code Playgroud)

在较新的编译器上,即使没有i32明确指定,您的代码也将按原样运行:

let x = 5;
println!("x = {}", x);
Run Code Online (Sandbox Code Playgroud)

您可以使用https://play.rust-lang.org/上的官方在线编译器,它始终是Rust的最新版本.