mod 导入时无效的 UTF-8 流
Invalid UTF-8 Stream on mod import
主要问题
如何在 main.rs
中使用来自 helper1.rs
的代码。
// main.rs
mod lib;
use lib::lib_function;
use lib::public_library;
mod module_a;
mod module_b;
use module_a::{helper1::helper1_function, mod_a_function};
use module_b::{helper2, helper3, mod_b_function};
fn main() {
println!("Hello, world from main!");
lib_function();
public_library::public_library_mod_function();
helper1_function();
mod_a_function();
helper2::helper2_function();
helper3::helper3_function();
mod_b_function();
}
// module_b/mod.rs
pub mod helper2;
pub mod helper3;
pub fn mod_a_function() {
println!("Printing from module_b/mod.rs");
}
// module_b/helper2.rs
pub fn helper2_function() {
println!("Hello from Helper 2!!");
}
我收到错误:
error: couldn't read src\module_b\mod.rs: stream did not contain valid UTF-8
--> src\main.rs:14:1
|
14 | mod module_b;
| ^^^^^^^^^^^^^
error: could not compile `rust_modules_cheat_sheet` due to previous error
我不确定 module_b
中但不在 module_a
中的非 utf-8 字符是什么。
作弊 Sheet 模块和导入 Rust
我在网上看到很多教程都说了同样的话,但对于其他情况来说并不完整或复杂。我想向社区询问并研究如何在 Rust 中使用来自各种情况和模块的代码。如果您想就我现在不确定的两种情况在下面发表评论,我们将不胜感激。
示例目录
/src
| main.rs
| lib.rs
| other.rs
|- module_a/
| | mod.rs
| | helper1.rs
|- module_b/
| | mod.rs
| | helper2.rs
| | helper3.rs
各种情况
- 在
main.rs
中使用来自 other.rs
的代码(理解)
- 使用
main.rs
中 lib.rs
的代码(已理解)
- 使用
lib.rs
中 mod.rs
的代码(已理解)
- 使用
main.rs
中 helper1.rs
的代码(已理解)
- 使用
helper3.rs
中 helper2.rs
的代码(不确定)
- 在
module_b/mod.rs
中使用来自 module_a/mod.rs
的代码(使用 crate::module_a)
Github 回购以备将来参考
Link to repo
希望这对将来的其他人有所帮助。我知道我需要它。
谢谢
这不是对 Rust 模块系统的误解。您的 module_b\mod.rs
文件是 UTF-16 编码的,而不是 Rust 期望的 UTF-8 编码。
如果您使用的是 VS Code: 您可以通过单击底部状态栏的 UTF-16 LE
部分并选择 Save with encoding
然后选择 [= 来更改编码13=].
如果使用 Vim:How can I change a file's encoding with vim?
主要问题
如何在 main.rs
中使用来自 helper1.rs
的代码。
// main.rs
mod lib;
use lib::lib_function;
use lib::public_library;
mod module_a;
mod module_b;
use module_a::{helper1::helper1_function, mod_a_function};
use module_b::{helper2, helper3, mod_b_function};
fn main() {
println!("Hello, world from main!");
lib_function();
public_library::public_library_mod_function();
helper1_function();
mod_a_function();
helper2::helper2_function();
helper3::helper3_function();
mod_b_function();
}
// module_b/mod.rs
pub mod helper2;
pub mod helper3;
pub fn mod_a_function() {
println!("Printing from module_b/mod.rs");
}
// module_b/helper2.rs
pub fn helper2_function() {
println!("Hello from Helper 2!!");
}
我收到错误:
error: couldn't read src\module_b\mod.rs: stream did not contain valid UTF-8
--> src\main.rs:14:1
|
14 | mod module_b;
| ^^^^^^^^^^^^^
error: could not compile `rust_modules_cheat_sheet` due to previous error
我不确定 module_b
中但不在 module_a
中的非 utf-8 字符是什么。
作弊 Sheet 模块和导入 Rust
我在网上看到很多教程都说了同样的话,但对于其他情况来说并不完整或复杂。我想向社区询问并研究如何在 Rust 中使用来自各种情况和模块的代码。如果您想就我现在不确定的两种情况在下面发表评论,我们将不胜感激。
示例目录
/src
| main.rs
| lib.rs
| other.rs
|- module_a/
| | mod.rs
| | helper1.rs
|- module_b/
| | mod.rs
| | helper2.rs
| | helper3.rs
各种情况
- 在
main.rs
中使用来自other.rs
的代码(理解) - 使用
main.rs
中lib.rs
的代码(已理解) - 使用
lib.rs
中mod.rs
的代码(已理解) - 使用
main.rs
中helper1.rs
的代码(已理解) - 使用
helper3.rs
中helper2.rs
的代码(不确定) - 在
module_b/mod.rs
中使用来自module_a/mod.rs
的代码(使用 crate::module_a)
Github 回购以备将来参考
Link to repo 希望这对将来的其他人有所帮助。我知道我需要它。
谢谢
这不是对 Rust 模块系统的误解。您的 module_b\mod.rs
文件是 UTF-16 编码的,而不是 Rust 期望的 UTF-8 编码。
如果您使用的是 VS Code:UTF-16 LE
部分并选择 Save with encoding
然后选择 [= 来更改编码13=].
如果使用 Vim:How can I change a file's encoding with vim?