0

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 in main.rs (Understood)
  • Using code from lib.rs in main.rs (Understood)
  • Using code from mod.rs in lib.rs (Understood)
  • Using code from helper1.rs in main.rs (Understood)
  • Using code from helper2.rs in helper3.rs (Unsure)
  • Using code from module_a/mod.rs in module_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

Lacrosse343
  • 491
  • 1
  • 3
  • 18
  • Please edit your question to ask a concrete question about a piece of code, which should be included in the question. – user4815162342 Dec 02 '21 at 18:47
  • This doesn't seem to be what you're asking about, but note that `mod lib;` is incorrect — `lib.rs` is the crate root for a library crate, and from a binary it should be referred to by the package name declared in Cargo.toml (e.g. `use rust_modules_cheat_sheet::lib_function;`), not by `mod`. Using `mod` means compiling the code twice in two contexts, which results in duplicated warnings/errors and other problems. – Kevin Reid Dec 02 '21 at 21:40
  • So that makes sense, but what does it mean when there is a main.rs and lib.rs? Is that still a library-style crate? Should all `mod` usage be done in lib.rs so that all binaries are pulled into the scope of lib.rs? @kevin-reid – Lacrosse343 Dec 03 '21 at 20:06
  • @Lacrosse343 If there is `lib.rs` and `main.rs` then Cargo will compile a library crate from `lib.rs` _and_ a binary crate from `main.rs`. The binary may `use` items from the library. – Kevin Reid Dec 03 '21 at 20:36

1 Answers1

0

This is not from a misunderstanding of Rust's module system. Your module_b\mod.rs file is UTF-16 encoded instead of UTF-8 encoded as Rust is expecting.

If you're using VS Code: Change the encoding of a file in Visual Studio Code You can change the encoding by clicking on the UTF-16 LE part of the bottom status bar and selecting Save with encoding and then selecting UTF-8.

If using Vim: How can I change a file's encoding with vim?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • Thanks, easy fix. I made this entire directory in VS, so I'm wondering what key or action i touched that made 3 of my files encoded as UTF-16 LE and the others encoded as UTF8. – Lacrosse343 Dec 03 '21 at 20:09