Rust - Include rust module in another directory - rust

This is my directory structure
src/
├── lib.rs
├── pages/
│ ├── mod.rs
│ ├── home_page.rs
└── components/
├── mod.rs
└── header.rs
Inside my pages/home_page.rs I try to access my pub struct Header which is inside components/header.rs.
My components/mod.rs looks like this: pub mod header; which works fine because inside lib.rs - I can use it like:
mod components;
use components::header::Header;
However, I don't know how to access it in pages/homepage.rs. How can get access to that struct? Is it something in Cargo.toml?

You can use a whole bunch of Rust keywords to navigate between the modules of your crate:
super::components::Header
// `super` is like a `parent` of your current mod
crate::components::Header
// `crate` is like a root of you current crate
And to include submodules of current mod:
self::submodule1::MyStruct
// `self` is like current module
You can read more about that here
Also it is good idea to make a prelude mod of your crate and include all main items of your crate there, so then you can include them just by passing use crate::prelude::*.
You can read more about prelude in offical rust docs and here.

Inside my src/pages/home_page.rs I can use my header like: use crate::components::header::Header;

Related

Cannot find crate on refactor

I am unable to understand why I cannot import a library into my file.
Here is what the folder looks like :
src/proofsystem/
├── proofsystem
│ ├── aggregation.rs
│ ├── mod.rs
│ └── simple.rs
└── mod.rs
In aggregation.rs , I import
use crate::proofsystem::{prepare_circuit_and_public_input, gen_pk};
use crate::proofsystem::ModelInput;
use solax::Address;
I want to move solax::Address to mod.rs, however the compiler can’t seem to find the imports.
/// Aggregation circuit
#[cfg(feature = “activate”)]
pub mod suffice;
use crate::commands::{data_path, Cli};
use crate::fieldutils::i32_to_felt;
use crate::graph::{utilities::vector_to_quantized, Model, ModelCircuit};
use crate::tensor::{Tensor, TensorType};
use solax::Address;
It is important to note that solax is is optional dependency in Cargo.toml.

How to include directories with Terragrunt

Is there a way to copy directories into the Terragrunt workspace?
I've come across the include_in_copy attritube, and I've tried something like this
terraform {
source = "git#gitlab.foo"
include_in_copy = [
"${get_parent_terragrunt_dir()}/foo-dir/"
]
}
But, it looks like this only copies content from whatever the source location is
However, in my case, I want to copy in an entire directory that's in the absolute path to the first terragrunt.hcl file, hence the use of the get_parent_terragrunt_dir() function.
This is what my file structure looks like.
├── deploy
│   └── bar
│   └── terragrunt.hcl
├── foo-dir
└── terragrunt.hcl
I'm calling terragrunt from inside the deploy/bar directory and I would like to include the foo-dir directory as well, but it looks like I'm doing something wrong.
Is there a function that might be able to do this?

Rust - importing root file to a module

I'm starting to learn Rust and I stumbled into a weird import problem. I have an example directory structure:
example_mod
file1.rs
example_mod.rs
file2.rs
I would like to import the contents of file2.rs into example_mod.rs. I tried:
use crate::file2;
use super::file2;
use file2;
And each time I get the use of undeclared type or module file2 error. How such sibling module should be imported?
Beginning new things is fun (and can be difficult). Now, your description of the problem you are having makes helping you kind of difficult. But, when you're starting out it can be hard to help yourself help-yourself. Check out how to create a MVCE.
Also check out what the Rust Book says about crates, modules, paths, and use. And, join the Rust Community if you want rust-specific help.
Okay, I'm making some assumptions about your code structure because you've left out a good deal of information.
So, if you run cargo new app you'll end up with a project folder containing a src/ directory.
I've added some files/directories and the result looks like this:
src/
├── core # a directory/folder
│ ├── mod.rs
│ └── some_other_code.rs # a "file"
├── main.rs
└── some_code.rs # another "file"
In main.rs:
/// Module declarations.
mod core;
mod some_code;
/// Bring paths into scope with the `use`
/// keyword keyword.
use self::some_code::hello;
// The above can also be written as:
// use some_code::hello;
// why?
/// An absolute path starts from a crate root by
/// using a crate name or a literal `crate`.
use crate::core::greeting;
const JUPITER: &str = "jupiter";
fn main() {
hello();
greeting();
// A relative path starts from the current
// module and uses self, super, or an identifier
// in the current module.
use self::another_mod;
// You can also check if the following would work:
// use crate::another_mod;
// Or even,
// use another_mod;
// why?
another_mod::jupiter();
}
mod another_mod {
use super::*;
pub fn jupiter() {
println!("hello, {}!", JUPITER);
}
}
In some_code.rs:
pub fn hello() {
println!("mars");
}
In core/mod.rs:
/// Module declaration.
pub mod some_other_code;
/// Re-exporting the greeting function.
pub use some_other_code::greeting;
In core/some_other_code.rs:
use crate::some_code;
pub fn greeting() {
println!("hello");
some_code::hello();
}
To give you an idea of what you'll find in the Rust Book:
Packages: A Cargo feature that lets you build, test, and share crates
Crates: A tree of modules that produces a library or executable
Modules and use: Let you control the organization, scope, and privacy of paths
Paths: A way of naming an item, such as a struct, function, or module
And,
src/main.rs and src/lib.rs are called crate roots. The reason for their name is that the contents of either of these two files form a module named crate at the root of the crate’s module structure, known as the module tree.
I've been through the book on this and ran that gamut. I personally don't like saying the following.
// *** main.rs
mod some_mod
use crate::some_mod::some_fn;
//rather just if you're going to
mod some_mod
use some_mod:some_fn;
//even further recall the binary name can see every mod that lives in main and lib
//so with the above you could move the mod to lib and keep main clean of mod
// *** lib.rs
pub mod some_mod
// then back in main just do
use rust_book::some_mod::some_fn;
// *** cargo.toml
[package]
name = "rust_book"
// Also keep in mind mods outside of lib will not be in cargo doc

Rust import error

I have a Rust project made by cargo init:
dir
|-src
|-main.rs
|-settings.rs
|-functions.rs
I have in settings.rs:
use ::functions;
but at compilation I get an error:
error[E0432]: unresolved import `functions`
--> src/settings.rs:3:5
|
3 | use ::functions;
| ^^^^^^^^^^^ no `functions` in the root
In your comments, you state:
I don't want to use [mod functions] because it will search for settings/functions.rs, and it is not i want to
Have you tried that? Assuming you've declared the module correctly ... this is exactly what you want.
main.rs:
mod functions;
mod settings;
fn main() {
...
}
settings.rs:
use functions;
pub fn something() {
functions::some_function_here();
}
If this does not work .. then there is something missing from your problem description.
It looks like you're confusing the role of the root namespace. use ::functions; means something slightly different than use functions;
Let's consider your project structure:
project
├── src
| ├── settings.rs
| ├── functions.rs
| └── main.rs
└── Cargo.toml
When you're within settings.rs, the other modules are at the same level, so a use functions; accesses the functions module.
If you do wish to refer to the functions module via the root namespace, then the full path looks like this ::project::functions and the use declaration is use ::project::functions;.

Move Diesel methods into other directories

I'm following the Diesel examples guide, and my project looks exactly like this. I want to change it so that instead of running cargo run --bin publish_post 1, you use cargo run and are presented with a loop prompting you for what action you want to run.
I've moved everything out of bin/ and into the controllers/ directory. I want to reference this in main.rs as use controllers::post, so I have access to post::delete(), etc.
Once I move the files out of bin/, all the imports break. Likewise, I can't reference it from lib.rs.
Why do none of my imports work when the files are moved? How I could access the methods from these files?
I want a structure like this:
├── controllers
│   └── posts.rs
├── lib.rs
├── main.rs
├── models.rs
├── schema.rs
And within main.rs, I want to be able to do something like:
use controllers::posts;
pub fn main() {
// pseudocode
loop {
println!("what action would you like to perform?");
let ans = capture_input();
if ans == "insert" {
posts::insert();
} else if ans == "delete" {
posts::delete();
}
}
}
Making a folder doesn't automatically make a Rust submodule. You need to do two things:
Declare the module explicitly in the crate root (lib.rs or main.rs):
mod controllers;
Create controllers/mod.rs file and declare a submodule in it:
mod posts;

Resources