Why "File not found for module" for sub-module? [duplicate] - rust

There's a lot of Rust documentation about using modules, but I haven't found an example of a Cargo binary that has multiple modules, with one module using another. My example has three files inside the src folder. Modules a and b are at the same level. One is not a submodule of another.
main.rs:
mod a;
fn main() {
println!("Hello, world!");
a::a();
}
a.rs:
pub fn a() {
println!("A");
b::b();
}
and b.rs:
pub fn b() {
println!("B");
}
I've tried variations of use b and mod b inside a.rs, but I cannot get this code to compile. If I try to use use b, for example, I get the following error:
--> src/a.rs:1:5
|
1 | use b;
| ^ no `b` in the root. Did you mean to use `a`?
What's the right way to have Rust recognize that I want to use module b from module a inside a cargo app?

You'll have to include b.rs somewhere, typically with mod b;. If b is a child of a (instead of being a sibling of a), there are two ways to do this:
Recommended: rename a.rs into a/mod.rs and b.rs into a/b.rs. Then you can mod b; in a/mod.rs.
Instead, you can just #[path = "b.rs"] mod b; in a.rs without renaming sources.
If b is intended to be a sibling of a (instead of being a child of a), you can just mod b; in main.rs and then use crate::b; in a.rs.

The method from the accepted answer doesn't work for me in Rust 1.33. Instead, I use the sibling module like this:
use crate::b;

In the latest Rust 1.63, we can use super keyword to refer siblings
// main.rs
mod a;
mod b;
fn main() {
}
// a.rs
pub fn a() {
println!("A");
super::b::b();
}
// b.rs
pub fn b() {
println!("B");
}

Related

Better way to call rust function in other file within the same directory?

I have a directory like this:
.
├── a.rs
├── b.rs
└── main.rs
main.rs:
mod a;
use a::a;
fn main() {
println!("Hello, world!");
a();
}
a.rs:
mod b;
use b::b;
pub fn a() {
b();
}
b.rs:
pub fn b() {
println!("b");
}
rustc complaints in a.rs about
file not found for module `b`
help: to create the module `b`, create file "src/a/b.rs" or "src/a/b/mod.rs"rustc(E0583)
If I put mod b; in main.rs, and replace mod b; with use crate::b::b; in a.rs, everything works well.
My question is: do I have to put every mod xxx; in the file where main() locates? I just want to call functions in the same-level rs files
When you put mod b; in a.rs, b will be considered as a submodule of module a. So, as suggested by rustc, it should be either in "src/a/b/mod.rs" or "src/a/b.rs". And when you put it instead in main.rs, it means that this module in located at the crate root, reason why it works without any problem because rustc looks for the file in the same directory as main.rs("src") and find it.
So, you don't have to put every mod xxx where main is located. If you creating an hierarchy (I mean modules and submodules and so on), you should respect the directory architecture and put mod xxx, only in the parent module, if any. You declare mod xxx in your main file for top level modules only.
You can see more explanation in the book :).

How do I correctly use traits in a file separate to its implementation

I'm having a bit of a headache trying to use traits defined in separate files to the implementation and was hoping somebody could point out where I am going wrong.
My file structure is this
main.rs
file1.rs
thing.rs
Contents of main.rs
mod file1;
mod thing;
fn main() {
let item : Box<dyn file1::Trait1> = Box::new(thing::Thing {});
}
file1.rs
pub trait Trait1 {
}
thing.rs
mod file1 {
include!("file1.rs");
}
pub struct Thing {
}
impl file1::Trait1 for Thing {
}
The error on compilation is:
error[E0277]: the trait bound `thing::Thing: file1::Trait1` is not satisfied
--> src/main.rs:9:41
|
9 | let item : Box<dyn file1::Trait1> = Box::new(thing::Thing {});
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `file1::Trait1` is not implemented for `thing::Thing`
|
= note: required for the cast to the object type `dyn file1::Trait1`
As far as I can tell file1::Trait1 is implemented. If not, what have I actually implemented?
mod file1 {
include!("file1.rs");
}
By writing this in thing.rs, you have created a module, thing::file1, which is distinct from the top-level module file1. Thus, you have two distinct versions of your trait, thing::file1::Trait1 and file1::Trait1.
This is almost never the right thing. As a general principle, every .rs file (except for main.rs, lib.rs, and other crate root files) should have exactly one mod declaration.
Delete the above code from thing.rs, and use use instead of mod, or a fully qualified path:
use crate::file1;
...
impl file1::Trait1 for Thing {
...
or
use crate::file1::Trait1;
...
impl Trait1 for Thing {
...
or
impl crate::file1::Trait1 for Thing {
...
In general, mod defines a module, and use brings items into scope. You write mod only once per module, and use wherever you want to refer to that module.

Confused on how to use rust modules [duplicate]

This question already has answers here:
Split a module across several files
(7 answers)
How can I include a module from another file from the same project?
(6 answers)
How to use one module from another module in a Rust cargo project?
(3 answers)
Closed 2 years ago.
I have three files. main.rs, foo.rs, bar.rs all in the src directory. I want to use whats in foo.rs in bar.rs. So i have mod foo; inside of bar. But i get an error as shown below. I do not want to put the foo.rs in a sub directory e.g. src/bar/foo.rs is there another way to structure the code? Because i want foo.rs to be used in many different places besides bar.rs. Optionally if you could tell me how you structure a big project with multiple files then that will suffice.
file not found for module `foo`
--> src/bar.rs:1:1
|
1 | mod foo;
| ^^^^^^^^
|
= help: to create the module `foo`, create file "src/bar/foo.rs"
error: aborting due to previous error
main.rs
mod bar;
fn main() {
println!("Hello, world!");
}
foo.rs
pub fn do_something() {
}
bar.rs
mod foo;
You only have to declare modules once, and in this case you would declare both in main and then you would be able to import them from anywhere else in your codebase using use crate::{mod_name}. Example:
src/main.rs
// declare foo & bar modules
mod foo;
mod bar;
fn main() {
foo::foo();
foo::call_bar();
bar::bar();
bar::call_foo();
}
src/foo.rs
// import bar module
use crate::bar;
pub fn foo() {
println!("foo");
}
pub fn call_bar() {
bar::bar();
}
src/bar.rs
// import foo module
use crate::foo;
pub fn bar() {
println!("bar");
}
pub fn call_foo() {
foo::foo();
}

Why do I need to "use std" in a module but not at the root of my crate? [duplicate]

I have the following code:
pub mod a {
#[test]
pub fn test() {
println!("{:?}", std::fs::remove_file("Somefilehere"));
}
}
I get errors when I compile this:
error[E0433]: failed to resolve. Use of undeclared type or module `std`
--> src/main.rs:4:24
|
4 | println!("{}", std::fs::remove_file("Somefilehere"));
| ^^^ Use of undeclared type or module `std`
However, removing the inner module and compiling the code it contains by itself works fine:
#[test]
pub fn test() {
println!("{:?}", std::fs::remove_file("Somefilehere"));
}
What am I missing here? I get the same errors if the module is in a separate file:
main.rs
pub mod a;
a.rs
#[test]
pub fn test() {
println!("{:?}", std::fs::remove_file("Somefilehere"));
}
By default, the compiler inserts extern crate std; at the beginning of the crate root (the crate root is the file that you pass to rustc). This statement has the effect of adding the name std to the crate's root namespace and associating it with a module that contains the public contents of the std crate.
However, in child modules, std is not automatically added in the module's namespace. This is why the compiler cannot resolve std (or anything that starts with std::) in a module.
There are many ways to fix this. First, you can add use std; in a module to make the name std refer, within that module, to the root std. Note that in use statements, the path is treated as absolute (or "relative to the crate's root namespace"), whereas everywhere else, paths are treated as relative to the current namespace (be it a module, a function, etc.).
pub mod a {
use std;
#[test]
pub fn test() {
println!("{:?}", std::fs::remove_file("Somefilehere"));
}
}
You can also use a use statement to import more specific items. For example, you can write use std::fs::remove_file;. This lets you avoid having to type the whole path to remove_file and just use the name remove_file directly within that module:
pub mod a {
use std::fs::remove_file;
#[test]
pub fn test() {
println!("{:?}", remove_file("Somefilehere")));
}
}
Finally, you can avoid using use altogether by prefixing the path with :: to ask the compiler to resolve the path from the crate's root namespace (i.e. turning the path into an absolute path).
pub mod a {
#[test]
pub fn test() {
println!("{:?}", ::std::fs::remove_file("Somefilehere"));
}
}
The recommended practice is to import types (structs, enums, etc.) directly (e.g. use std::rc::Rc;, then use the path Rc), but to use functions through an import of their parent module (e.g. use std::io::fs;, then use the path fs::remove_file).
pub mod a {
use std::fs;
#[test]
pub fn test() {
println!("{:?}", fs::remove_file("Somefilehere"));
}
}
Side note: You can also write self:: at the beginning of a path to make it relative to the current module. This is more often used in use statements, since other paths are already relative (though they are relative to the current namespace, whereas self:: is always relative to the containing module).
Nowadays, std is directly accessible from everywhere, so the code you showed is compiling as you would expect.
Furthermore, extern crate is no longer needed in Rust edition 2018. Adding a dependency to Cargo.toml makes the crate name directly available as a top-level identifier.

include module from the same directory level [duplicate]

If you have a directory structure like this:
src/main.rs
src/module1/blah.rs
src/module1/blah2.rs
src/utils/logging.rs
How do you use functions from other files?
From the Rust tutorial, it sounds like I should be able to do this:
main.rs
mod utils { pub mod logging; }
mod module1 { pub mod blah; }
fn main() {
utils::logging::trace("Logging works");
module1::blah::doit();
}
logging.rs
pub fn trace(msg: &str) {
println!(": {}\n", msg);
}
blah.rs
mod blah2;
pub fn doit() {
blah2::doit();
}
blah2.rs
mod utils { pub mod logging; }
pub fn doit() {
utils::logging::trace("Blah2 invoked");
}
However, this produces an error:
error[E0583]: file not found for module `logging`
--> src/main.rs:1:21
|
1 | mod utils { pub mod logging; }
| ^^^^^^^
|
= help: name the file either logging.rs or logging/mod.rs inside the directory "src/utils"
It appears that importing down the path, i.e. from main to module1/blah.rs works, and importing peers, i.e. blah2 from blah works, but importing from the parent scope doesn't.
If I use the magical #[path] directive, I can make this work:
blah2.rs
#[path="../utils/logging.rs"]
mod logging;
pub fn doit() {
logging::trace("Blah2 invoked");
}
Do I really have to manually use relative file paths to import something from a parent scope level? Isn't there some better way of doing this in Rust?
In Python, you use from .blah import x for the local scope, but if you want to access an absolute path you can use from project.namespace.blah import x.
I'm going to answer this question too, for anyone else who finds this and is (like me) totally confused by the difficult-to-comprehend answers.
It boils down to two things I feel are poorly explained in the tutorial:
The mod blah; syntax imports a file for the compiler. You must use this on all the files you want to compile.
As well as shared libraries, any local module that is defined can be imported into the current scope using use blah::blah;.
A typical example would be:
src/main.rs
src/one/one.rs
src/two/two.rs
In this case, you can have code in one.rs from two.rs by using use:
use two::two; // <-- Imports two::two into the local scope as 'two::'
pub fn bar() {
println!("one");
two::foo();
}
However, main.rs will have to be something like:
use one::one::bar; // <-- Use one::one::bar
mod one { pub mod one; } // <-- Awkwardly import one.rs as a file to compile.
// Notice how we have to awkwardly import two/two.rs even though we don't
// actually use it in this file; if we don't, then the compiler will never
// load it, and one/one.rs will be unable to resolve two::two.
mod two { pub mod two; }
fn main() {
bar();
}
Notice that you can use the blah/mod.rs file to somewhat alleviate the awkwardness, by placing a file like one/mod.rs, because mod x; attempts x.rs and x/mod.rs as loads.
// one/mod.rs
pub mod one.rs
You can reduce the awkward file imports at the top of main.rs to:
use one::one::bar;
mod one; // <-- Loads one/mod.rs, which loads one/one.rs.
mod two; // <-- This is still awkward since we don't two, but unavoidable.
fn main() {
bar();
}
There's an example project doing this on Github.
It's worth noting that modules are independent of the files the code blocks are contained in; although it would appear the only way to load a file blah.rs is to create a module called blah, you can use the #[path] to get around this, if you need to for some reason. Unfortunately, it doesn't appear to support wildcards, aggregating functions from multiple files into a top-level module is rather tedious.
I'm assuming you want to declare utils and utils::logging at the top level, and just wish to call functions from them inside module1::blah::blah2. The declaration of a module is done with mod, which inserts it into the AST and defines its canonical foo::bar::baz-style path, and normal interactions with a module (away from the declaration) are done with use.
// main.rs
mod utils {
pub mod logging { // could be placed in utils/logging.rs
pub fn trace(msg: &str) {
println!(": {}\n", msg);
}
}
}
mod module1 {
pub mod blah { // in module1/blah.rs
mod blah2 { // in module1/blah2.rs
// *** this line is the key, to bring utils into scope ***
use crate::utils;
pub fn doit() {
utils::logging::trace("Blah2 invoked");
}
}
pub fn doit() {
blah2::doit();
}
}
}
fn main() {
utils::logging::trace("Logging works");
module1::blah::doit();
}
The only change I made was the use crate::utils; line in blah2 (in Rust 2015 you could also use use utils or use ::utils). Also see the second half of this answer for more details on how use works. The relevant section of The Rust Programming Language is a reasonable reference too, in particular these two subsections:
Separating Modules into Different Files
Bringing Paths into Scope with the use Keyword
Also, notice that I write it all inline, placing the contents of foo/bar.rs in mod foo { mod bar { <contents> } } directly, changing this to mod foo { mod bar; } with the relevant file available should be identical.
(By the way, println(": {}\n", msg) prints two new lines; println! includes one already (the ln is "line"), either print!(": {}\n", msg) or println!(": {}", msg) print only one.)
It's not idiomatic to get the exact structure you want, you have to make one change to the location of blah2.rs:
src
├── main.rs
├── module1
│   ├── blah
│   │   └── blah2.rs
│   └── blah.rs
└── utils
└── logging.rs
main.rs
mod utils {
pub mod logging;
}
mod module1 {
pub mod blah;
}
fn main() {
utils::logging::trace("Logging works");
module1::blah::doit();
}
utils/logging.rs
pub fn trace(msg: &str) {
println!(": {}\n", msg);
}
module1/blah.rs
mod blah2;
pub fn doit() {
blah2::doit();
}
module1/blah/blah2.rs (the only file that requires any changes)
// this is the only change
// Rust 2015
// use utils;
// Rust 2018
use crate::utils;
pub fn doit() {
utils::logging::trace("Blah2 invoked");
}
I realize this is a very old post and probably wasn't using 2018. However, this can still be really tricky and I wanted to help those out that were looking.
Because Pictures are worth a thousand words I made this simple for code splitting.
Then as you probably guessed they all have an empty pub fn some_function().
We can further expand on this via the changes to main
The additional changes to nested_mod
Let's now go back and answer the question:
We added blah1 and blah2 to the mod_1
We added a utils with another mod logging inside it that calls some fn's.
Our mod_1/mod.rs now contains:
pub mod blah.rs
pub mod blah2.rs
We created a utils/mod.rs used in main containing:
pub mod logging
Then a directory called logging/with another mod.rs where we can put fns in logging to import.
Source also here https://github.com/DavidWhit/Rust_Modules
Also Check Chapters 7 for libs example and 14.3 that further expands splitting with workspaces in the Rust Book. Good Luck!
Answers here were unclear for me so I will put my two cents for future Rustaceans.
All you need to do is to declare all files via mod in src/main.rs (and fn main of course).
// src/main.rs
mod module1 {
pub mod blah;
pub mod blah2;
}
mod utils {
pub mod logging;
}
fn main () {
module1::blah::doit();
}
Make everything public with pub you need to use externally (copy-pasted original src/utils/logging.rs). And then simply use declared modules via crate::.
// src/module1/blah.rs
use crate::utils::logging;
// or `use crate::utils` and `utils::logging::("log")`, however you like
pub fn doit() {
logging::trace("Logging works");
}
p.s. I shuffled functions a bit for a cleaner answer.
If you create a file called mod.rs, rustc will look at it when importing a module. I would suggest that you create the file src/utils/mod.rs, and make its contents look something like this:
pub mod logging;
Then, in main.rs, add a statement like this:
use utils::logging;
and call it with
logging::trace(...);
or you could do
use utils::logging::trace;
...
trace(...);
Basically, declare your module in the mod.rs file, and use it in your source files.

Resources