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;








83















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.










share|improve this question





















  • 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

















83















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.










share|improve this question





















  • 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













83












83








83


13






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












2 Answers
2






active

oldest

votes


















163
















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!");






share|improve this answer



























  • 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, 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



















10
















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()`");






share|improve this answer






















  • 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











  • 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













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
);



);














draft saved

draft discarded
















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









163
















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!");






share|improve this answer



























  • 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, 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
















163
















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!");






share|improve this answer



























  • 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, 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














163














163










163









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!");






share|improve this answer















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!");







share|improve this answer














share|improve this answer



share|improve this answer








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, 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


















  • 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, 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

















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














10
















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()`");






share|improve this answer






















  • 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











  • 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















10
















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()`");






share|improve this answer






















  • 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











  • 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













10














10










10









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()`");






share|improve this answer















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()`");







share|improve this answer














share|improve this answer



share|improve this answer








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 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












  • @Gandhi See The path attribute

    – Gardener
    Jul 24 at 11:41












  • 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











  • 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


















draft saved

draft discarded















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현