Module 为什么不是';这个模块不可见吗?

Module 为什么不是';这个模块不可见吗?,module,rust,visibility,Module,Rust,Visibility,我正在用Rust编写一个哈希,以备练习。代码如下所示: pub fn get_fnv1a32(to_hash:&str) -> u32{ const OFFSET_BASIS:u32 = 2_166_136_261; const PRIME:u32 = 16_777_619; if !to_hash.is_empty(){ let mut hash = OFFSET_BASIS; for b in to_hash.bytes(){

我正在用Rust编写一个哈希,以备练习。代码如下所示:

pub fn get_fnv1a32(to_hash:&str) -> u32{
   const OFFSET_BASIS:u32 = 2_166_136_261;
   const PRIME:u32 = 16_777_619;

   if !to_hash.is_empty(){
      let mut hash = OFFSET_BASIS;
      for b in to_hash.bytes(){
         hash = hash ^ (b as u32);
         hash = hash.wrapping_mul(PRIME);
      }
      hash
   }
   else
   {
      0
   }
}
这是我试图用来测试的代码:

mod fnv;
#[cfg(test)]
mod tests {
    #[test]
    fn get_correct_hash(){
        assert_eq!(0x7a78f512, fnv::get_fnv1a32("Hello world!"));
    } 

    #[test]
    fn hash_handles_empty_string_correctly(){
        assert_eq!(0, fnv::get_fnv1a32(""));
    }
 } 
测试代码在lib.rs中,
get\u fnv1a32
函数在fnv.rs中。它们都在同一个目录中。但当我尝试运行货物测试时,我不断收到以下消息:

Compiling hashes v0.1.0 (U:\skunkworks\rust\hashes)
warning: function is never used: `get_fnv1a32`
 --> src\fnv.rs:1:8
  |
1 | pub fn get_fnv1a32(to_hash:&str) -> u32{
  |        ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

error[E0433]: failed to resolve: use of undeclared type or module `fnv`
 --> src\lib.rs:7:32
  |
7 |         assert_eq!(0x7a78f512, fnv::get_fnv1a32("Hello world!"));
  |                                ^^^ use of undeclared type or module `fnv`

error[E0433]: failed to resolve: use of undeclared type or module `fnv`
  --> src\lib.rs:12:23
   |
12 |         assert_eq!(0, fnv::get_fnv1a32(""));
   |                       ^^^ use of undeclared type or module `fnv`

error: aborting due to 2 previous errors
我不知道我做错了什么。我试着改变
mod fnv顶部的行到<代码>发布模块fnv
这样就消除了死代码警告,但它不能修复这两个错误。我需要做什么才能使函数在lib.rs文件中可见


我并不认为这有什么关系,但rustc的版本是rustc 1.41.0(5e1a79984 2020-01-27)

测试模块与外部模块分开。加

use super::*;

或者类似于
测试
模块内使用claret::fnv
这样的等效语句,使
fnv
模块可见。

这就解决了问题,但您能为我澄清一些事情吗。“与外部模块分离”是什么意思?与文件系统类似,像
fnv::get_fnv1a32
这样的名称的功能类似于相对路径:如果您位于板条箱(lib.rs)的根模块中,它类似于
/
,则有一个子模块(类似于子目录)
fnv
和一个子模块
测试
。要从测试子模块中引用函数
fnv::get_fnv1a32
,基本上必须使用
super
,这类似于说
。/fnv/get_fnv1a32
,或者
crater
,这类似于说
/fnv/get_fnv1a32
。这有用吗?