在Rust,如何使用Rand等模块,您不包括孩子模块?

发布于 2025-02-13 10:32:14 字数 447 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2025-02-20 10:32:14

如果thread_rng()是RNG模块的本地...

rng不是模块,而是一个特征。

thread_rngrand模块的函数。

请参阅 rand docs

if thread_rng() is local to the Rng module...

Rng is not a module, it is a trait.

thread_rng is a function of the rand module.

See the rand docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文