-3

Consider a Rust "project" consisting of 3 files: main.rs, dyn1.rs, and dyn2.rs.

main.rs. wants to use functions (e.g. "diag") in dyn1.rs and constants (e.g FIFTY) in dyn2.rs

dyn1.rs wants to use constants (e.g. FIFTY) in dyn2.rs

dyn2.rs consists of simple constant definitions like: pub const FIFTY:i32 = 50;

But I want to use FIFTY in both main.rs and dyn1.rs

For simplicity of this question, I don't want the solution to employ any "use" statements. I will add those later after I understand the "mod" solution alone. [My question was closed because of a similar but different question (How do I import from a sibling module?---linked here). But that question and the answers given all focus on the "use" statement. They do not address doing what I want only using "mod" statements.]

I think I have to employ "mod" statements.

MY QUESTIONS

(1) What exact mod statements should go main.rs and dyn1.rs, so that main can use both dyn1 and dyn2 items and so that dyn1 can use dyn2 items?

(2) How would I call fn diag in main.rs? What goes in front of the ::?

(3) How would I use FIFTY in an expression in a function in main.rs? let x = 3 + ::FIFTY; What goes in front of the ::?

(4) How would I use FIFTY in an expression in a function in dyn1.rs? let x = 3 + ::FIFTY; What goes in front of the ::?

(5) Finally, can my src directory just consist of main.rs, dyn1.rs, and dyn2.rs? Or must I have subdirectories? I don't consider dyn2 a submodule of dyn1, as least logically.

(6) Afterthought: is the pub keyword necessary anywhere in this solution?

I've tried every approach I can pull together from looking at the documentation, but I must be missing something.

In main.rs, I've tried using:

mod dyn1;
mod dyn2;
dyn1::diag(...);
let x = 3 + dyn2::FIFTY;

In dyn1.rs, I've tried using:

mod dyn2;
let y = 3 + dyn2::FIFTY;

Updated on Jan 29, 2022. I've found that path statement in the code snippet below seems to solve the problem. For some reason I don't yet understand, such a statement is not needed in main.rs, but it is needed in dyn1.rs.

#[path = "./dyn2.rs"]
mod dyn2;
let y = 3 + dyn2::FIFTY;

1 Answers1

-1

(1) What exact mod statements should go main.rs and dyn1.rs, so that main can use both dyn1 and dyn2 items and so that dyn1 can use dyn2 items?

You should be using mod in the main.rs as follows

 mod dyn1;
 mod dyn2;

Also, in dyn1.rs add the mod dyn2;.

(2) How would I call fn diag in main.rs? What goes in front of the ::?

To call the diag function in main.rs use dyn1::diag(...);

(3) How would I use FIFTY in an expression in main.rs? let x = 3 + ::FIFTY; What goes in front of the ::?

You cant use let outside of a function, if you are intending to us eit isnide a function you can use it as let x = 3 + dyn2::FIFTY;

(4) How would I use FIFTY in an expression in dyn1.rs? let x = 3 + ::FIFTY; What goes in front of the ::?

exactly same as in #3

(5) Finally, can my src directory just consist of main.rs, dyn1.rs, and dyn2.rs? Or must I have subdirectories? I don't consider dyn2 a submodule of dyn1, as least logically.

You dont need any subdirectories

(6) Afterthought: is the pub keyword necessary anywhere in this solution?

Pub keyword is required as it would allow you to use the const FIFTY from other modules. If its not public main.rs and dyn1.rs cant use it

dmSherazi
  • 3,743
  • 5
  • 37
  • 62