Main Question
How to use code from helper1.rs
in main.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!!");
}
I get the error:
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
I'm not sure what non-utf-8 character is in module_b
but isn't in module_a
.
Cheat Sheet for modules and importing in Rust
I've seen a lot of tutorials online that kind of say the same thing, but it's not complete or sophisticated for other cases. I wanted to ask the community as well as research how to use code from various situations and modules in rust. If you would like to comment below for the two cases I am unsure of right now, that would be appreciated.
Example Directory
/src
| main.rs
| lib.rs
| other.rs
|- module_a/
| | mod.rs
| | helper1.rs
|- module_b/
| | mod.rs
| | helper2.rs
| | helper3.rs
Various Situation
- Using code from
other.rs
inmain.rs
(Understood) - Using code from
lib.rs
inmain.rs
(Understood) - Using code from
mod.rs
inlib.rs
(Understood) - Using code from
helper1.rs
inmain.rs
(Understood) - Using code from
helper2.rs
inhelper3.rs
(Unsure) - Using code from
module_a/mod.rs
inmodule_b/mod.rs
(use crate::module_a)
Github Repo for Future Reference
Link to repo Hopefully, this helps others in the future. I know I need it.
Thanks