为什么这个模块不可见?
Why Isn't This Module Visible?
我正在用 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_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;
行更改为 pub mod fnv;
,这消除了死代码警告,但它没有修复这两个错误。我需要做什么才能使 get_fnv1a32
函数在 lib.rs 文件中可见?
我认为这并不重要,但 rustc 的版本是 rustc 1.41.0 (5e1a79984 2020-01-27)
测试模块与外部模块是分开的。添加
use super::*;
或 tests
模块内的 use crate::fnv
等等效语句,使 fnv
模块可见。
我正在用 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_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;
行更改为 pub mod fnv;
,这消除了死代码警告,但它没有修复这两个错误。我需要做什么才能使 get_fnv1a32
函数在 lib.rs 文件中可见?
我认为这并不重要,但 rustc 的版本是 rustc 1.41.0 (5e1a79984 2020-01-27)
测试模块与外部模块是分开的。添加
use super::*;
或 tests
模块内的 use crate::fnv
等等效语句,使 fnv
模块可见。