Importing functions that are in a different directory - rust

I am having trouble importing function from a different directory into my lib.rs in rust. I posted my code and file structure below.
However, when I put the get_users.rs into routes and add pub mod get_users; to src/routes/mod.rs the server spins up and the get users work.
File Structure
.
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│ ├── lib.rs
│ ├── main.rs
│ └── routes
│ ├── echo.rs
│ └── mod.rs
| |__ users
| |__ get_users.rs
| |-- mod.rs
├── static
│ └── abruzzo.png
└── tests
└── basic_test.rs
Error:
error[E0433]: failed to resolve: could not find `users` in `routes`
--> src\lib.rs:15:17
|
15 | routes::users::get_users::get_users
| ^^^^^ could not find `users` in `routes`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `rocket-mongo` due to previous error
main.rs
use rocket_mongo::rocket_builder;
fn main() {
crate::rocket_builder().launch();
}
lib.rs
#![feature(proc_macro_hygiene, decl_macro)]
#![allow(unused_attributes)]
#[macro_use] use rocket::*;
use rocket_contrib::helmet::SpaceHelmet;
mod routes;
pub fn rocket_builder() -> rocket::Rocket {
rocket::ignite().attach(SpaceHelmet::default())
.mount("/", routes![
routes::echo::echo_fn,
routes::ping::ping_fn,
routes::users::get_users::get_users
])
}
src/routes/users/get_users.rs
use rocket::*;
#[get("/users")]
pub fn get_users() -> String {
"List of users".to_string()
}
src/routes/users/mod.rs
pub mod get_users;
Things I have tried:
Adding mod routes::users; to lib.rs
Adding use routes::users:get_users;
Edit: I dont know how to reply to comments but Jakub Dóka answered me by saying I needed to add pub mod users; in my src/routes/mod.rs file. Thank you so much Jakub!

Related

Scrypto Importing Structs from other folder [duplicate]

This question already has answers here:
How can I include a module from another file from the same project?
(6 answers)
Closed 7 months ago.
In Scrypto, I have folder structure like:
src/
├── lib.rs
├── script.rs
├── custom_types
│ └── type.rs
In type.rs, I have the following defined:
use sbor::*;
#[derive(TypeId, Encode, Decode, Describe)]
pub struct Date {
year: u8,
day: u8,
month: u8,
}
I want to be able to use the Date struct in script.rs how do I do it?
Organize files like:
src/
├── lib.rs
├── script.rs
├── custom_types
│ └── type.rs
│ └── mod.rs
mod.rs:
pub mod type;
Add the following to script.rs:
use super::custom_types::type::Date;
...
Finally, add the following to lib.rs
mod datatypes;

How to import a function from another folder in the folder above

My directory structure looks like this
src
├── main.rs
├── paas_core
│   ├── create.rs
│   └── mod.rs
└── rest_api
├── mod.rs
└── rest.rs
I want to use a function declared in file create.rs in the file called rest.rs. I am trying to use the module in the rest_api module but I can not figure out how to do it.
I tried using super::paas_core in rest.rs but that didn't work.
In the file rest_api/rest.rs do you have to use use crate::paas_core; in order to be able to reference from rest_api to paas_core and paas_core/mod.rs need the statement pub mod create; to ensure you can call the function from rest_api directly using paas_core::create::create_func();
I hope it explains it. An full example might look like this:
file: main.rs
mod paas_core;
mod rest_api;
fn main() {
rest_api::rest_func();
}
file: rest_api/mod.rs
mod rest;
pub fn rest_func() {
println!("rest_func() in rest_api/mod.rs called!");
rest::rest_caller();
}
file: rest_api/rest.rs
use crate::paas_core;
pub fn rest_caller() {
println!("rest_caller() in rest_api/rest.rs called!");
paas_core::create::create_func();
}
file: paas_core/mod.rs
pub mod create;
file: paas_core/create.rs
pub fn create_func() {
println!("create_func() in paas_core/create.rs finally called!");
}
output if you are running it:
rest_func() in rest_api/mod.rs called!
rest_caller() in rest_api/rest.rs called!
create_func() in paas_core/create.rs finally called!

How Rust finds its dependencies [duplicate]

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?
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 in a module.
To include the code from hello.rs in 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!");
}
If you wish to have nested modules...
Rust 2018
It's no longer required to have the file mod.rs (although it is still supported). The idiomatic alternative is to name the file the name of the module:
$ tree src
src
├── main.rs
├── my
│   ├── inaccessible.rs
│   └── nested.rs
└── my.rs
main.rs
mod my;
fn main() {
my::function();
}
my.rs
pub mod nested; // if you need to include other modules
pub fn function() {
println!("called `my::function()`");
}
Rust 2015
You need to put a mod.rs file inside your folder of the same name as your module. Rust by Example explains it better.
$ tree src
src
├── main.rs
└── my
├── inaccessible.rs
├── mod.rs
└── nested.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()`");
}
I really like Gardener's response. I've been using the suggestion for my module declarations.
./src
├── main.rs
├── other_utils
│ └── other_thing.rs
└── utils
└── thing.rs
File main.rs
#[path = "utils/thing.rs"] mod thing;
#[path = "other_utils/other_thing.rs"] mod other_thing;
fn main() {
thing::foo();
other_thing::bar();
}
File utils/thing.rs
pub fn foo() {
println!("foo");
}
File other_utils/other_thing.rs
#[path = "../utils/thing.rs"] mod thing;
pub fn bar() {
println!("bar");
thing::foo();
}
In a non main.rs (or lib.rs) file if you want to include from a file in the same directory then the code below works. The key is to use the word super:: for the include. (This is how I rewrote the answer of rodo without using path.)
Directory tree:
src
├── main.rs
├── my.rs
└── my
├── a.rs
└── b.rs
To include a.rs in b.rs:
File src/my/a.rs
pub fn function() {
println!("src/my/a.rs/function()");
}
File src/my/b.rs
use super::b::function;
fn f2() {
function();
}
File src/my.rs
mod a;
mod b;
File src/main.rs
mod my;
As of 2022
├── src
├── main.rs
├── scripts
│ └── func.rs
└── scripts.rs
If I want to call the functions from the func.rs file (which are inside the scripts folder), I created a so-called "linking" file in the root directory the same as the folder's name (it's not necessary to have the linking file name and the folder name the same).
Content of file scripts/func.rs:
pub fn sayHello(){
println!("Hello, World!");
}
In the scripts.rs file I have:
pub(crate) mod func;
Then in my main.rs file I have called the sayHello() function as below:
mod scripts;
fn main() {
scripts::func::sayHello();
}
its a faily simple approach in solving this, you need to add mod into the main.rs file, for its to be accessible throughout the file tree

Rustlang book 14.3 - error[E0425]: cannot find function `add_one` in crate `add_one`

I'm working through the official Rustlang book and have reached chapter 14.3. However, once I reach a certain point of building the workspace, the compiler throws an error. My project looks like this (as per the book)
├── Cargo.lock
├── Cargo.toml
├── add-one
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
├── adder
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── target
with Cargo.toml
[workspace]
members = [
"adder",
"add-one",
]
add-one/src/lib.rs
#![allow(unused)]
fn main() {
pub fn add_one(x: i32) -> i32 {
x + 1
}
}
adder/Cargo.toml
...
[dependencies]
add-one = { path = "../add-one" }
adder/src/main.rs
use add_one;
fn main() {
let num = 10;
println!(
"Hello, world! {} plus one is {}!",
num,
add_one::add_one(num)
);
}
I initially tried typing the files out, but got the above error, so decided to copy/paste all the files from the book, but no luck there either. What am I missing? Is there a problem with my code, or is the example in the book faulty?
You are defining add_one inside the main function:
#![allow(unused)]
fn main() {
pub fn add_one(x: i32) -> i32 {
x + 1
}
}
Nested functions are limited in scope to their parent, meaning that add_one is not visible outside of the main. You can fix this by declaring add_one in module scope:
pub fn add_one(x: i32) -> i32 {
x + 1
}

How can I include a module from another file from the same project?

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?
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 in a module.
To include the code from hello.rs in 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!");
}
If you wish to have nested modules...
Rust 2018
It's no longer required to have the file mod.rs (although it is still supported). The idiomatic alternative is to name the file the name of the module:
$ tree src
src
├── main.rs
├── my
│   ├── inaccessible.rs
│   └── nested.rs
└── my.rs
main.rs
mod my;
fn main() {
my::function();
}
my.rs
pub mod nested; // if you need to include other modules
pub fn function() {
println!("called `my::function()`");
}
Rust 2015
You need to put a mod.rs file inside your folder of the same name as your module. Rust by Example explains it better.
$ tree src
src
├── main.rs
└── my
├── inaccessible.rs
├── mod.rs
└── nested.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()`");
}
I really like Gardener's response. I've been using the suggestion for my module declarations.
./src
├── main.rs
├── other_utils
│ └── other_thing.rs
└── utils
└── thing.rs
File main.rs
#[path = "utils/thing.rs"] mod thing;
#[path = "other_utils/other_thing.rs"] mod other_thing;
fn main() {
thing::foo();
other_thing::bar();
}
File utils/thing.rs
pub fn foo() {
println!("foo");
}
File other_utils/other_thing.rs
#[path = "../utils/thing.rs"] mod thing;
pub fn bar() {
println!("bar");
thing::foo();
}
In a non main.rs (or lib.rs) file if you want to include from a file in the same directory then the code below works. The key is to use the word super:: for the include. (This is how I rewrote the answer of rodo without using path.)
Directory tree:
src
├── main.rs
├── my.rs
└── my
├── a.rs
└── b.rs
To include a.rs in b.rs:
File src/my/a.rs
pub fn function() {
println!("src/my/a.rs/function()");
}
File src/my/b.rs
use super::b::function;
fn f2() {
function();
}
File src/my.rs
mod a;
mod b;
File src/main.rs
mod my;
As of 2022
├── src
├── main.rs
├── scripts
│ └── func.rs
└── scripts.rs
If I want to call the functions from the func.rs file (which are inside the scripts folder), I created a so-called "linking" file in the root directory the same as the folder's name (it's not necessary to have the linking file name and the folder name the same).
Content of file scripts/func.rs:
pub fn sayHello(){
println!("Hello, World!");
}
In the scripts.rs file I have:
pub(crate) mod func;
Then in my main.rs file I have called the sayHello() function as below:
mod scripts;
fn main() {
scripts::func::sayHello();
}
its a faily simple approach in solving this, you need to add mod into the main.rs file, for its to be accessible throughout the file tree

Resources