代码之家  ›  专栏  ›  技术社区  ›  spicy-lemonade

在rust中,为什么像rand这样的模块不包括子模块?

  •  -1
  • spicy-lemonade  · 技术社区  · 2 年前

    use rand::Rng

    当你想使用它时,你可以键入 let 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

    1 回复  |  直到 2 年前
        1
  •  0
  •   Schwern    2 年前

    如果thread\u rng()是rng模块的本地线程。。。

    Rng 这不是一个模块,而是一种特质。

    thread_rng rand 单元

    请参阅 rand docs .