How to include module from another file from the same project?How do I “use” or import a local Rust file?Why rust can't find a function in a submodule?Accessing subdirectories in rust using mod keywordWhy can't I import module from different file in same directory?Required to use module name twice to reference a struct in the moduleBasic modularity issue with .rs files [rust]Rust: unresolved import `X`: no `X` in the rootRust file not found for module error for file in same directorySplit a module across several filesHow do I do a basic import/include of a function from one module to another in Rust 2015?How do I compile a multi-file crate in Rust?Access functions from sub-module from other sub-moduleCargo.toml OS Dependency for CrateInclude Module RustHow to divide my program in modules?How to use one module from another module in a Rust cargo project?Including a file from another that is not main.rs nor lib.rs
Why didn't Thor use the All powerful spear instead of Stormbreaker?
Are programming languages necessary/useful for operations research practitioner?
Minimizing proper time
Gas pipes - why does gas burn "outwards?"
How does Vivi differ from other Black Mages?
What does 鴨が葱を背負って来る mean?
How do I improve my SXA sites Google PageSpeed Insights Score?
Relevance of the Resurrection
Which currencies does Wizz Air use in-flight?
Is English tonal for some words, like "permit"?
Is there a star over my head?
Renewed US passport, did not receive expired US passport
Honourable versus Right Honourable Members
Where does the expression "triple-A" comes from?
Why was "leaping into the river" a valid trial outcome to prove one's innocence?
I changed a word from a source, how do I cite it correctly?
My research paper filed as a patent in China by my Chinese supervisor without me as inventor
Can a magnet rip protons from a nucleus?
Sol Ⅲ = Earth: What is the origin of this planetary naming scheme?
Is it possible to PIVOT on a LIKE statement
Is BitLocker useful in the case of stolen laptop?
How can a resurrection system prevent the cheapening of death?
Do Milankovitch Cycles fully explain climate change?
Are the definite and indefinite integrals actually two different things? Where is the flaw in my understanding?
How to include module from another file from the same project?
How do I “use” or import a local Rust file?Why rust can't find a function in a submodule?Accessing subdirectories in rust using mod keywordWhy can't I import module from different file in same directory?Required to use module name twice to reference a struct in the moduleBasic modularity issue with .rs files [rust]Rust: unresolved import `X`: no `X` in the rootRust file not found for module error for file in same directorySplit a module across several filesHow do I do a basic import/include of a function from one module to another in Rust 2015?How do I compile a multi-file crate in Rust?Access functions from sub-module from other sub-moduleCargo.toml OS Dependency for CrateInclude Module RustHow to divide my program in modules?How to use one module from another module in a Rust cargo project?Including a file from another that is not main.rs nor lib.rs
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
By following this guide I created a Cargo project.
src/main.rs
fn main()
hello::print_hello();
mod hello
pub fn print_hello()
println!("Hello, world!");
which I run using
cargo build && cargo run
and it compiles without errors. Now I'm trying to split the main module in two but cannot figure out how to include a module from another file.
My project tree looks like this
├── src
├── hello.rs
└── main.rs
and the content of the files:
src/main.rs
use hello;
fn main()
hello::print_hello();
src/hello.rs
mod hello
pub fn print_hello()
println!("Hello, world!");
When I compile it with cargo build
I get
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
I tried to follow the compiler's suggestions and modified main.rs
to:
#![feature(globs)]
extern crate hello;
use hello::*;
fn main()
hello::print_hello();
But this still doesn't help much, now I get this:
error[E0463]: can't find crate for `hello`
--> src/main.rs:3:1
|
3 | extern crate hello;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
Is there a trivial example of how to include one module from the current project into the project's main file?
Also, I'm running Rust 1.37.0.
rust
add a comment |
By following this guide I created a Cargo project.
src/main.rs
fn main()
hello::print_hello();
mod hello
pub fn print_hello()
println!("Hello, world!");
which I run using
cargo build && cargo run
and it compiles without errors. Now I'm trying to split the main module in two but cannot figure out how to include a module from another file.
My project tree looks like this
├── src
├── hello.rs
└── main.rs
and the content of the files:
src/main.rs
use hello;
fn main()
hello::print_hello();
src/hello.rs
mod hello
pub fn print_hello()
println!("Hello, world!");
When I compile it with cargo build
I get
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
I tried to follow the compiler's suggestions and modified main.rs
to:
#![feature(globs)]
extern crate hello;
use hello::*;
fn main()
hello::print_hello();
But this still doesn't help much, now I get this:
error[E0463]: can't find crate for `hello`
--> src/main.rs:3:1
|
3 | extern crate hello;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
Is there a trivial example of how to include one module from the current project into the project's main file?
Also, I'm running Rust 1.37.0.
rust
1
possible duplicate of Rust basic imports (includes)
– Levans
Oct 15 '14 at 18:52
Related to stackoverflow.com/questions/22596920/…
– Kelvin
Jul 31 '15 at 15:50
add a comment |
By following this guide I created a Cargo project.
src/main.rs
fn main()
hello::print_hello();
mod hello
pub fn print_hello()
println!("Hello, world!");
which I run using
cargo build && cargo run
and it compiles without errors. Now I'm trying to split the main module in two but cannot figure out how to include a module from another file.
My project tree looks like this
├── src
├── hello.rs
└── main.rs
and the content of the files:
src/main.rs
use hello;
fn main()
hello::print_hello();
src/hello.rs
mod hello
pub fn print_hello()
println!("Hello, world!");
When I compile it with cargo build
I get
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
I tried to follow the compiler's suggestions and modified main.rs
to:
#![feature(globs)]
extern crate hello;
use hello::*;
fn main()
hello::print_hello();
But this still doesn't help much, now I get this:
error[E0463]: can't find crate for `hello`
--> src/main.rs:3:1
|
3 | extern crate hello;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
Is there a trivial example of how to include one module from the current project into the project's main file?
Also, I'm running Rust 1.37.0.
rust
By following this guide I created a Cargo project.
src/main.rs
fn main()
hello::print_hello();
mod hello
pub fn print_hello()
println!("Hello, world!");
which I run using
cargo build && cargo run
and it compiles without errors. Now I'm trying to split the main module in two but cannot figure out how to include a module from another file.
My project tree looks like this
├── src
├── hello.rs
└── main.rs
and the content of the files:
src/main.rs
use hello;
fn main()
hello::print_hello();
src/hello.rs
mod hello
pub fn print_hello()
println!("Hello, world!");
When I compile it with cargo build
I get
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
I tried to follow the compiler's suggestions and modified main.rs
to:
#![feature(globs)]
extern crate hello;
use hello::*;
fn main()
hello::print_hello();
But this still doesn't help much, now I get this:
error[E0463]: can't find crate for `hello`
--> src/main.rs:3:1
|
3 | extern crate hello;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
Is there a trivial example of how to include one module from the current project into the project's main file?
Also, I'm running Rust 1.37.0.
rust
rust
edited Aug 25 at 14:38
Lukas Kalbertodt
32.3k5 gold badges81 silver badges146 bronze badges
32.3k5 gold badges81 silver badges146 bronze badges
asked Oct 15 '14 at 17:47
aveave
4,9135 gold badges19 silver badges32 bronze badges
4,9135 gold badges19 silver badges32 bronze badges
1
possible duplicate of Rust basic imports (includes)
– Levans
Oct 15 '14 at 18:52
Related to stackoverflow.com/questions/22596920/…
– Kelvin
Jul 31 '15 at 15:50
add a comment |
1
possible duplicate of Rust basic imports (includes)
– Levans
Oct 15 '14 at 18:52
Related to stackoverflow.com/questions/22596920/…
– Kelvin
Jul 31 '15 at 15:50
1
1
possible duplicate of Rust basic imports (includes)
– Levans
Oct 15 '14 at 18:52
possible duplicate of Rust basic imports (includes)
– Levans
Oct 15 '14 at 18:52
Related to stackoverflow.com/questions/22596920/…
– Kelvin
Jul 31 '15 at 15:50
Related to stackoverflow.com/questions/22596920/…
– Kelvin
Jul 31 '15 at 15:50
add a comment |
2 Answers
2
active
oldest
votes
You don't need the mod hello
in your hello.rs
file. Code in any file but the crate root (main.rs
for executables, lib.rs
for libraries) is automatically namespaced on a module.
To include the code from hello.rs
on your main.rs
, use mod hello;
. It gets expanded to the code that is in hello.rs
(exactly as you had before). Your file structure continues the same, and your code needs to be slightly changed:
main.rs
:
mod hello;
fn main()
hello::print_hello();
hello.rs
:
pub fn print_hello()
println!("Hello, world!");
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
6
@ChristianSchmitt No, they are different things.use
is just a namespace thing, whilemod
pulls in the file. You would useuse
, for example, to be able to call theprint_hello
function without having to prefix with the namespace
– Renato Zannon
Sep 16 '15 at 19:08
add a comment |
You need the mod.rs
file in your folder. Rust by Example explains it better.
$ tree .
.
|-- my
| |-- inaccessible.rs
| |-- mod.rs
| |-- nested.rs
`-- split.rs
main.rs
mod my;
fn main()
my::function();
mod.rs
pub mod nested; //if you need to include other modules
pub fn function()
println!("called `my::function()`");
2
Suppose I wanted to use something frominaccessible.rs
innested.rs
... how would I do that?
– Heman Gandhi
Jun 30 at 20:00
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:#[path = "inaccessible.rs"]
and on the next line:mod inaccessible;
– Gardener
Jul 24 at 11:26
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26388861%2fhow-to-include-module-from-another-file-from-the-same-project%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need the mod hello
in your hello.rs
file. Code in any file but the crate root (main.rs
for executables, lib.rs
for libraries) is automatically namespaced on a module.
To include the code from hello.rs
on your main.rs
, use mod hello;
. It gets expanded to the code that is in hello.rs
(exactly as you had before). Your file structure continues the same, and your code needs to be slightly changed:
main.rs
:
mod hello;
fn main()
hello::print_hello();
hello.rs
:
pub fn print_hello()
println!("Hello, world!");
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
6
@ChristianSchmitt No, they are different things.use
is just a namespace thing, whilemod
pulls in the file. You would useuse
, for example, to be able to call theprint_hello
function without having to prefix with the namespace
– Renato Zannon
Sep 16 '15 at 19:08
add a comment |
You don't need the mod hello
in your hello.rs
file. Code in any file but the crate root (main.rs
for executables, lib.rs
for libraries) is automatically namespaced on a module.
To include the code from hello.rs
on your main.rs
, use mod hello;
. It gets expanded to the code that is in hello.rs
(exactly as you had before). Your file structure continues the same, and your code needs to be slightly changed:
main.rs
:
mod hello;
fn main()
hello::print_hello();
hello.rs
:
pub fn print_hello()
println!("Hello, world!");
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
6
@ChristianSchmitt No, they are different things.use
is just a namespace thing, whilemod
pulls in the file. You would useuse
, for example, to be able to call theprint_hello
function without having to prefix with the namespace
– Renato Zannon
Sep 16 '15 at 19:08
add a comment |
You don't need the mod hello
in your hello.rs
file. Code in any file but the crate root (main.rs
for executables, lib.rs
for libraries) is automatically namespaced on a module.
To include the code from hello.rs
on your main.rs
, use mod hello;
. It gets expanded to the code that is in hello.rs
(exactly as you had before). Your file structure continues the same, and your code needs to be slightly changed:
main.rs
:
mod hello;
fn main()
hello::print_hello();
hello.rs
:
pub fn print_hello()
println!("Hello, world!");
You don't need the mod hello
in your hello.rs
file. Code in any file but the crate root (main.rs
for executables, lib.rs
for libraries) is automatically namespaced on a module.
To include the code from hello.rs
on your main.rs
, use mod hello;
. It gets expanded to the code that is in hello.rs
(exactly as you had before). Your file structure continues the same, and your code needs to be slightly changed:
main.rs
:
mod hello;
fn main()
hello::print_hello();
hello.rs
:
pub fn print_hello()
println!("Hello, world!");
edited Aug 25 at 14:40
Lukas Kalbertodt
32.3k5 gold badges81 silver badges146 bronze badges
32.3k5 gold badges81 silver badges146 bronze badges
answered Oct 15 '14 at 18:57
Renato ZannonRenato Zannon
19.9k6 gold badges25 silver badges34 bronze badges
19.9k6 gold badges25 silver badges34 bronze badges
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
6
@ChristianSchmitt No, they are different things.use
is just a namespace thing, whilemod
pulls in the file. You would useuse
, for example, to be able to call theprint_hello
function without having to prefix with the namespace
– Renato Zannon
Sep 16 '15 at 19:08
add a comment |
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
6
@ChristianSchmitt No, they are different things.use
is just a namespace thing, whilemod
pulls in the file. You would useuse
, for example, to be able to call theprint_hello
function without having to prefix with the namespace
– Renato Zannon
Sep 16 '15 at 19:08
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
Late Question wouldn't it also work if I specify it with use hello instead of mod hello?!
– Christian Schmitt
Sep 16 '15 at 17:23
6
6
@ChristianSchmitt No, they are different things.
use
is just a namespace thing, while mod
pulls in the file. You would use use
, for example, to be able to call the print_hello
function without having to prefix with the namespace– Renato Zannon
Sep 16 '15 at 19:08
@ChristianSchmitt No, they are different things.
use
is just a namespace thing, while mod
pulls in the file. You would use use
, for example, to be able to call the print_hello
function without having to prefix with the namespace– Renato Zannon
Sep 16 '15 at 19:08
add a comment |
You need the mod.rs
file in your folder. Rust by Example explains it better.
$ tree .
.
|-- my
| |-- inaccessible.rs
| |-- mod.rs
| |-- nested.rs
`-- split.rs
main.rs
mod my;
fn main()
my::function();
mod.rs
pub mod nested; //if you need to include other modules
pub fn function()
println!("called `my::function()`");
2
Suppose I wanted to use something frominaccessible.rs
innested.rs
... how would I do that?
– Heman Gandhi
Jun 30 at 20:00
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:#[path = "inaccessible.rs"]
and on the next line:mod inaccessible;
– Gardener
Jul 24 at 11:26
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
add a comment |
You need the mod.rs
file in your folder. Rust by Example explains it better.
$ tree .
.
|-- my
| |-- inaccessible.rs
| |-- mod.rs
| |-- nested.rs
`-- split.rs
main.rs
mod my;
fn main()
my::function();
mod.rs
pub mod nested; //if you need to include other modules
pub fn function()
println!("called `my::function()`");
2
Suppose I wanted to use something frominaccessible.rs
innested.rs
... how would I do that?
– Heman Gandhi
Jun 30 at 20:00
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:#[path = "inaccessible.rs"]
and on the next line:mod inaccessible;
– Gardener
Jul 24 at 11:26
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
add a comment |
You need the mod.rs
file in your folder. Rust by Example explains it better.
$ tree .
.
|-- my
| |-- inaccessible.rs
| |-- mod.rs
| |-- nested.rs
`-- split.rs
main.rs
mod my;
fn main()
my::function();
mod.rs
pub mod nested; //if you need to include other modules
pub fn function()
println!("called `my::function()`");
You need the mod.rs
file in your folder. Rust by Example explains it better.
$ tree .
.
|-- my
| |-- inaccessible.rs
| |-- mod.rs
| |-- nested.rs
`-- split.rs
main.rs
mod my;
fn main()
my::function();
mod.rs
pub mod nested; //if you need to include other modules
pub fn function()
println!("called `my::function()`");
edited Mar 25 '18 at 14:54
Shepmaster
179k22 gold badges397 silver badges565 bronze badges
179k22 gold badges397 silver badges565 bronze badges
answered Mar 25 '18 at 13:33
amxaamxa
1713 silver badges4 bronze badges
1713 silver badges4 bronze badges
2
Suppose I wanted to use something frominaccessible.rs
innested.rs
... how would I do that?
– Heman Gandhi
Jun 30 at 20:00
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:#[path = "inaccessible.rs"]
and on the next line:mod inaccessible;
– Gardener
Jul 24 at 11:26
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
add a comment |
2
Suppose I wanted to use something frominaccessible.rs
innested.rs
... how would I do that?
– Heman Gandhi
Jun 30 at 20:00
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:#[path = "inaccessible.rs"]
and on the next line:mod inaccessible;
– Gardener
Jul 24 at 11:26
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
2
2
Suppose I wanted to use something from
inaccessible.rs
in nested.rs
... how would I do that?– Heman Gandhi
Jun 30 at 20:00
Suppose I wanted to use something from
inaccessible.rs
in nested.rs
... how would I do that?– Heman Gandhi
Jun 30 at 20:00
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:
#[path = "inaccessible.rs"]
and on the next line: mod inaccessible;
– Gardener
Jul 24 at 11:26
To access a sibling .rs file from a file other than main.rs, use the path attribute. So, at the top of nested.rs, add the following:
#[path = "inaccessible.rs"]
and on the next line: mod inaccessible;
– Gardener
Jul 24 at 11:26
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
@Gandhi See The path attribute
– Gardener
Jul 24 at 11:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26388861%2fhow-to-include-module-from-another-file-from-the-same-project%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
possible duplicate of Rust basic imports (includes)
– Levans
Oct 15 '14 at 18:52
Related to stackoverflow.com/questions/22596920/…
– Kelvin
Jul 31 '15 at 15:50