如何在Rust println中直接使用函数返回值 [英] How to use function return value directly in Rust println

查看:355
本文介绍了如何在Rust println中直接使用函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust允许通过以下方式格式化变量的打印:

Rust allows formatted printing of variables this way:

fn main(){
  let r:f64 = rand::random();
  println!("{}",r);
}

但这不起作用:

fn main(){
  println!("{}",rand::random());
}

它显示此错误:

   |
31 |   println!("{}",rand::random());
   |                 ^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the function `random`

是否可以直接在println!中使用函数返回值?

Is it possible to use function return value directly with println!?

推荐答案

Rust不知道rand::random应该是什么类型,因此可以使用涡轮鱼提供类型提示:

Rust doesn't know what type rand::random should be, so you can use the turbofish to provide a type hint:

println!("{}", rand::random::<f64>());

这篇关于如何在Rust println中直接使用函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆