Rust 学习 0

时间:2022-09-07 15:28:09
  • 安装Rust 后,本地有文档: file:///usr/local/share/doc/rust/html/index.html file:///usr/local/share/doc/rust/html/rand/index.html
  • Rust 每行末尾都需要分号“;”,有时挺繁琐的
  • guessing_gname 中
extern crate rand;

let secret_number = rand::thread_rng().gen_range(1, 101);

但忘记 use rand::Rng;,编译失败:

Compiling guessing_gname v0.1.0 (file:///Users/Simon/WorkSpaces/Rust/guessing_gname)
src/main.rs:9:44: 9:68 error: type `rand::ThreadRng` does not implement any method in scope named `gen_range`
src/main.rs:9 let secret_number = rand::thread_rng().gen_range::<i32>(1, 101);
^~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:9:44: 9:68 help: methods from traits can only be called if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
src/main.rs:9:44: 9:68 help: candidate #1: use `rand::Rng`
error: aborting due to previous error
Could not compile `guessing_gname`. To learn more, run the command again with --verbose.

代码中没有明确使用 rand::Rng,编译器应该自动推导才对,不该让程序员来主动导入。或使用工具自动导入。