How to use functions from one file among multiple files?












0















I'm trying to use the functions from one file with multiple other files.



When I try adding 'mod somefile' to the files, the Rust compiler wants them to be nested in a subfolder, which isn't how I want to structure the project, as it would mean duplicating the file each time.



// src/main.rs
mod aaa;
mod bbb;

fn main() {
aaa::do_something();
bbb::do_something_else();
}


// src/aaa.rs
mod zzz; // rust compiler wants the file to be nested in a subfolder as aaa/zzz.rs

pub fn do_something() {
zzz::do_stuff();
}


// src/bbb.rs
mod zzz; // again, compiler wants the file nested in a subfolder as bbb/zzz.rs

pub fn do_something_else() {
zzz::do_stuff();
}


// src/zzz.rs

pub fn do_stuff() {
// does stuff here
}


I would like to be able to leave src/zzz.rs in the root src folder and use its functions among any of the other files in the project, instead of having to duplicate it in subfolders for each file (ex: src/aaa/zzz.rs, src/bbb/zzz.rs).










share|improve this question

























  • There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates.

    – BitTickler
    Jan 18 at 20:12











  • Possible duplicate of Rust basic imports (includes)

    – hellow
    Jan 18 at 21:21











  • @BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"?

    – Andrey Tyukin
    Jan 18 at 21:22













  • @AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that.

    – BitTickler
    Jan 18 at 21:46
















0















I'm trying to use the functions from one file with multiple other files.



When I try adding 'mod somefile' to the files, the Rust compiler wants them to be nested in a subfolder, which isn't how I want to structure the project, as it would mean duplicating the file each time.



// src/main.rs
mod aaa;
mod bbb;

fn main() {
aaa::do_something();
bbb::do_something_else();
}


// src/aaa.rs
mod zzz; // rust compiler wants the file to be nested in a subfolder as aaa/zzz.rs

pub fn do_something() {
zzz::do_stuff();
}


// src/bbb.rs
mod zzz; // again, compiler wants the file nested in a subfolder as bbb/zzz.rs

pub fn do_something_else() {
zzz::do_stuff();
}


// src/zzz.rs

pub fn do_stuff() {
// does stuff here
}


I would like to be able to leave src/zzz.rs in the root src folder and use its functions among any of the other files in the project, instead of having to duplicate it in subfolders for each file (ex: src/aaa/zzz.rs, src/bbb/zzz.rs).










share|improve this question

























  • There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates.

    – BitTickler
    Jan 18 at 20:12











  • Possible duplicate of Rust basic imports (includes)

    – hellow
    Jan 18 at 21:21











  • @BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"?

    – Andrey Tyukin
    Jan 18 at 21:22













  • @AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that.

    – BitTickler
    Jan 18 at 21:46














0












0








0








I'm trying to use the functions from one file with multiple other files.



When I try adding 'mod somefile' to the files, the Rust compiler wants them to be nested in a subfolder, which isn't how I want to structure the project, as it would mean duplicating the file each time.



// src/main.rs
mod aaa;
mod bbb;

fn main() {
aaa::do_something();
bbb::do_something_else();
}


// src/aaa.rs
mod zzz; // rust compiler wants the file to be nested in a subfolder as aaa/zzz.rs

pub fn do_something() {
zzz::do_stuff();
}


// src/bbb.rs
mod zzz; // again, compiler wants the file nested in a subfolder as bbb/zzz.rs

pub fn do_something_else() {
zzz::do_stuff();
}


// src/zzz.rs

pub fn do_stuff() {
// does stuff here
}


I would like to be able to leave src/zzz.rs in the root src folder and use its functions among any of the other files in the project, instead of having to duplicate it in subfolders for each file (ex: src/aaa/zzz.rs, src/bbb/zzz.rs).










share|improve this question
















I'm trying to use the functions from one file with multiple other files.



When I try adding 'mod somefile' to the files, the Rust compiler wants them to be nested in a subfolder, which isn't how I want to structure the project, as it would mean duplicating the file each time.



// src/main.rs
mod aaa;
mod bbb;

fn main() {
aaa::do_something();
bbb::do_something_else();
}


// src/aaa.rs
mod zzz; // rust compiler wants the file to be nested in a subfolder as aaa/zzz.rs

pub fn do_something() {
zzz::do_stuff();
}


// src/bbb.rs
mod zzz; // again, compiler wants the file nested in a subfolder as bbb/zzz.rs

pub fn do_something_else() {
zzz::do_stuff();
}


// src/zzz.rs

pub fn do_stuff() {
// does stuff here
}


I would like to be able to leave src/zzz.rs in the root src folder and use its functions among any of the other files in the project, instead of having to duplicate it in subfolders for each file (ex: src/aaa/zzz.rs, src/bbb/zzz.rs).







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 4:02









Shepmaster

151k14296434




151k14296434










asked Jan 18 at 20:08









A. S. UnderwoodA. S. Underwood

1




1













  • There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates.

    – BitTickler
    Jan 18 at 20:12











  • Possible duplicate of Rust basic imports (includes)

    – hellow
    Jan 18 at 21:21











  • @BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"?

    – Andrey Tyukin
    Jan 18 at 21:22













  • @AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that.

    – BitTickler
    Jan 18 at 21:46



















  • There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates.

    – BitTickler
    Jan 18 at 20:12











  • Possible duplicate of Rust basic imports (includes)

    – hellow
    Jan 18 at 21:21











  • @BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"?

    – Andrey Tyukin
    Jan 18 at 21:22













  • @AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that.

    – BitTickler
    Jan 18 at 21:46

















There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates.

– BitTickler
Jan 18 at 20:12





There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates.

– BitTickler
Jan 18 at 20:12













Possible duplicate of Rust basic imports (includes)

– hellow
Jan 18 at 21:21





Possible duplicate of Rust basic imports (includes)

– hellow
Jan 18 at 21:21













@BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"?

– Andrey Tyukin
Jan 18 at 21:22







@BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"?

– Andrey Tyukin
Jan 18 at 21:22















@AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that.

– BitTickler
Jan 18 at 21:46





@AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that.

– BitTickler
Jan 18 at 21:46












1 Answer
1






active

oldest

votes


















4














You need mod zzz; only once in the main.rs, in aaa.rs and bbb.rs you need a use zzz;, not a mod zzz;. This here works as advertised:



File aaa.rs:



use zzz;

pub fn do_something() {
zzz::do_stuff();
}


File bbb.rs:



use zzz;

pub fn do_something_else() {
zzz::do_stuff();
}


File main.rs:



// src/main.rs
mod aaa;
mod bbb;
mod zzz;

fn main() {
aaa::do_something();
bbb::do_something_else();
}


File zzz.rs:



pub fn do_stuff() {
println!("does stuff zzz");
}




You would need a mod zzz; inside of aaa module only if you had a directory called aaa and inside of it a files mod.rs and zzz.rs. Then you would have to put mod zzz; in mod.rs to make the submodule aaa::zzz visible to the rest of your program.






share|improve this answer























    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/3.0/"u003ecc by-sa 3.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%2f54260772%2fhow-to-use-functions-from-one-file-among-multiple-files%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    You need mod zzz; only once in the main.rs, in aaa.rs and bbb.rs you need a use zzz;, not a mod zzz;. This here works as advertised:



    File aaa.rs:



    use zzz;

    pub fn do_something() {
    zzz::do_stuff();
    }


    File bbb.rs:



    use zzz;

    pub fn do_something_else() {
    zzz::do_stuff();
    }


    File main.rs:



    // src/main.rs
    mod aaa;
    mod bbb;
    mod zzz;

    fn main() {
    aaa::do_something();
    bbb::do_something_else();
    }


    File zzz.rs:



    pub fn do_stuff() {
    println!("does stuff zzz");
    }




    You would need a mod zzz; inside of aaa module only if you had a directory called aaa and inside of it a files mod.rs and zzz.rs. Then you would have to put mod zzz; in mod.rs to make the submodule aaa::zzz visible to the rest of your program.






    share|improve this answer




























      4














      You need mod zzz; only once in the main.rs, in aaa.rs and bbb.rs you need a use zzz;, not a mod zzz;. This here works as advertised:



      File aaa.rs:



      use zzz;

      pub fn do_something() {
      zzz::do_stuff();
      }


      File bbb.rs:



      use zzz;

      pub fn do_something_else() {
      zzz::do_stuff();
      }


      File main.rs:



      // src/main.rs
      mod aaa;
      mod bbb;
      mod zzz;

      fn main() {
      aaa::do_something();
      bbb::do_something_else();
      }


      File zzz.rs:



      pub fn do_stuff() {
      println!("does stuff zzz");
      }




      You would need a mod zzz; inside of aaa module only if you had a directory called aaa and inside of it a files mod.rs and zzz.rs. Then you would have to put mod zzz; in mod.rs to make the submodule aaa::zzz visible to the rest of your program.






      share|improve this answer


























        4












        4








        4







        You need mod zzz; only once in the main.rs, in aaa.rs and bbb.rs you need a use zzz;, not a mod zzz;. This here works as advertised:



        File aaa.rs:



        use zzz;

        pub fn do_something() {
        zzz::do_stuff();
        }


        File bbb.rs:



        use zzz;

        pub fn do_something_else() {
        zzz::do_stuff();
        }


        File main.rs:



        // src/main.rs
        mod aaa;
        mod bbb;
        mod zzz;

        fn main() {
        aaa::do_something();
        bbb::do_something_else();
        }


        File zzz.rs:



        pub fn do_stuff() {
        println!("does stuff zzz");
        }




        You would need a mod zzz; inside of aaa module only if you had a directory called aaa and inside of it a files mod.rs and zzz.rs. Then you would have to put mod zzz; in mod.rs to make the submodule aaa::zzz visible to the rest of your program.






        share|improve this answer













        You need mod zzz; only once in the main.rs, in aaa.rs and bbb.rs you need a use zzz;, not a mod zzz;. This here works as advertised:



        File aaa.rs:



        use zzz;

        pub fn do_something() {
        zzz::do_stuff();
        }


        File bbb.rs:



        use zzz;

        pub fn do_something_else() {
        zzz::do_stuff();
        }


        File main.rs:



        // src/main.rs
        mod aaa;
        mod bbb;
        mod zzz;

        fn main() {
        aaa::do_something();
        bbb::do_something_else();
        }


        File zzz.rs:



        pub fn do_stuff() {
        println!("does stuff zzz");
        }




        You would need a mod zzz; inside of aaa module only if you had a directory called aaa and inside of it a files mod.rs and zzz.rs. Then you would have to put mod zzz; in mod.rs to make the submodule aaa::zzz visible to the rest of your program.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 21:13









        Andrey TyukinAndrey Tyukin

        27.6k42349




        27.6k42349






























            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%2f54260772%2fhow-to-use-functions-from-one-file-among-multiple-files%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

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre