dat*_*uoc 5 printing return function formatted-text rust
Rust 允许以这种方式格式化打印变量:
fn main(){
let r:f64 = rand::random();
println!("{}",r);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
fn main(){
println!("{}",rand::random());
}
Run Code Online (Sandbox Code Playgroud)
它显示这个错误:
|
31 | println!("{}",rand::random());
| ^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the function `random`
Run Code Online (Sandbox Code Playgroud)
是否可以直接使用函数返回值println!?
Rust 不知道rand::random应该是什么类型,所以你可以使用turbofish来提供类型提示:
println!("{}", rand::random::<f64>());
Run Code Online (Sandbox Code Playgroud)