我正在遵循rust lang网站上的学习rust书,随机数生成不起作用。
具体地说,当尝试像这样创建随机范围时:
use rand::Rng;
fn main() {
let s: u32 = rand::thread_rng().gen_range(1, 101);
println!("{}", s);
}我得到了错误:
Checking learn-rust v0.1.0 (.../learn-rust)
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> src/main.rs:8:37
|
8 | let s: u32 = rand::thread_rng().gen_range(1, 101);
| ^^^^^^^^^ - --- supplied 2 arguments
| |
| expected 1 argument
error: aborting due to previous error
For more information about this error, try `rustc --explain E0061`.
error: could not compile `learn-rust`
To learn more, run the command again with --verbose.在rust playground中,我得到了同样的错误?
本地和游乐场上的rand版本都是0.8.0。
发布于 2020-12-24 07:01:12
解决此类错误的最简单方法是使用search for gen_range in the docs for the crate (rand)。您会发现它是一个范围,它只有一个参数:method of the Rng trait。因此,为它提供一个范围:
rand::thread_rng().gen_range(1..101)https://stackoverflow.com/questions/65431949
复制相似问题