在Rust,如何使用Rand等模块,您不包括孩子模块?
在Rust中,您可以通过键入使用rand :: rng
在使用它时使用模块,
当您要使用它时,您可以键入让x = rand :: thread_rng()。gen_range(0,100);
如果thread_rng()
是RNG模块的本地rng :: thread_rng()。gen_range(0,100);
如果我尝试类似的东西。
mod first{
pub mod second{
pub fn hello(){println!("hello");}
}
}
use first::second;
fn main(){
first::hello();
}
那么我会遇到一个错误,说它在模块中找不到Hello Hello
In rust you can use a module by typing use rand::Rng
when you want to use it you type let x = rand::thread_rng().gen_range(0,100);
if thread_rng()
is local to the Rng module then how come we dont type Rng::thread_rng().gen_range(0,100);
if I try something similar..
mod first{
pub mod second{
pub fn hello(){println!("hello");}
}
}
use first::second;
fn main(){
first::hello();
}
then I get an error saying it cant find hello in module first
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rng
不是模块,而是一个特征。thread_rng
是rand
模块的函数。请参阅
rand
docs 。Rng
is not a module, it is a trait.thread_rng
is a function of therand
module.See the
rand
docs.