Confusion with mod something to import sibling file [duplicate] - rust

This question already has answers here:
How to include files from same directory in a module using Cargo/Rust?
(3 answers)
How do I do a basic import/include of a function from one module to another in Rust 2015?
(3 answers)
Why can't I import module from different file in same directory? [duplicate]
(2 answers)
How do I import from a sibling module?
(1 answer)
Closed 1 year ago.
according to https://stackoverflow.com/a/26435155/5884503, mod something searches for something.rs.
Here's what I'm trying to do based on that:
main.rs
message_socket.rs
mod.rs
body.rs
mod.rs:
mod message_socket;
mod body;
body.rs:
struct Body{}
message_socket.rs:
mod body;
error:
error[E0583]: file not found for module `body`
--> src/message_socket.rs:7:1
|
7 | mod body;
| ^^^^^^^^^
|
= help: to create the module `body`, create file "src/message_socket/body.rs"
It's trying to look inside message_socket which is a file, not a directory.
Why?

Related

How can I check type of an instance in Rust? [duplicate]

This question already has answers here:
How does Rust implement reflection?
(1 answer)
How do I print in Rust the type of a variable?
(17 answers)
Closed 8 months ago.
I can not find a built-in function to check the type of my param in this function (Rust):
pub fn notify<T: Summary + Display>(item: &T) {
// type of "item" (Summary || Display)
}

Module not found in rust [duplicate]

This question already has answers here:
How do I import from a sibling module?
(1 answer)
How to use one module from another module in a Rust cargo project?
(3 answers)
Split a module across several files
(7 answers)
Closed 2 years ago.
Welcome, this question is for sure repeated, but for past 2 hours my mod didn't work and im sure I've done everything correctly.
Here is my tree:
project \
|- target...
|
|- rust_stuff...
|
\- src
|- main.rs
|
|- first_file.rs
|
\- second_file.rs
And here is second one, for reference when i will list what i tried:
project \
|- target...
|
|- rust_stuff...
|
\- src
|- main.rs
|
|- first_file.rs
|
\- cool_folder
|
\second_file.rs
Ok, so we have:
// -- MAIN --
mod first_file;
pub use first_file::*;
fn main() {
call_first();
}
Now, first_file.rs contains
// -- FIRST_FILE --
mod second_file; //E: Not found
pub use second_file; //E: Not used
pub fn call_first(){
call_second(); //E: Out of scope
}
I will stop here, second_file exists, and cannot be accesed by first_file but can be accessed by main just like this:
// -- MAIN --
mod first_file;
pub use first_file::*;
mod second_file;
pub use second_file::*;
fn main() {
call_first(); //Would call 'call_second' but as i presented it can't
call_second(); //Returns "Hello world" in terminal
}
(Thats assuming second_file is same as first_file with just different function naming)
Now, reffering to second tree i showed, i tried putting those files in cool_file, but it worked out the same except i used:
mod cool_file; //Err
pub use cool_file::*;
// this does not work
pub use second_file::*;
// neither does this
In documentation from what i understand, i wrote it the right way, in regards to first example, i have no idea what to do. Please help.

Why Rust compiler didn't stop me from mutating const data type using smart pointer RefCell? [duplicate]

This question already has answers here:
Why do changes to a const variable not persist between usages?
(2 answers)
How do global consts that are not copy or clone work in Rust?
(1 answer)
What does it mean for a constant value in Rust to be inlined?
(3 answers)
What is the difference between a constant and a static variable and which should I choose?
(4 answers)
Closed 2 years ago.
I was surprised to see the smart pointer RefCell can mutate any type of data as I expect to mutate even const. Why const data cannot be mutated via smart pointers? If I considered the const can never be changed then why the attempt to modify const data executed successfully?
const NAME : RefCell<&str> = RefCell::new("XYZ");
println!("The const NAME is: {}",*NAME.borrow_mut());
*NAME.borrow_mut() = "abc";
println!("The mutated NAME is: {}",*NAME.borrow());
I exactly want to know why the const data is not modified even the compiler executed the code successfully. I expect the compiler will stop me from doing that modification but actually it didn't? Here is the output,
The name is: XYZ
The mutated name is: XYZ

Do I have to write the whole type of the iterator? [duplicate]

This question already has answers here:
How to write a Rust function that takes an iterator?
(3 answers)
What is the correct way to return an Iterator (or any other trait)?
(2 answers)
Closed 3 years ago.
Let's say I have an iterator like this:
let it = text.char_indices().take(x).skip(y);
Then I want to pass it to a function my_func. Currently I have a parameter like this:
fn my_func(it: std::iter::Skip<std::iter::Take<std::str::CharIndices>>) {
}
Is there a shorter way to write the type? Also, the function shouldn't really require that all iterator parameters now must be derived from .skip and .take and in this exact order.

Static lifetime for a temporary variable? [duplicate]

This question already has answers here:
How to convert a String into a &'static str
(4 answers)
How can you make a safe static singleton in Rust?
(3 answers)
How do I create a global, mutable singleton?
(7 answers)
Closed 3 years ago.
I have the following function:
fn load_data() -> Result<MyData> {
// ...
Ok(my_data)
}
I want to call this function only once during the execution of my program and save the result in a static variable.
It'd be nice if the result is of type Option<MyData>, so the consumer just needs to check if the data is there or not, regardless of the reason of why it's not there.
I have came up with the following:
lazy_static! {
static ref DATA: Option<&'static MyData> = load_data().ok().as_ref();
}
However, that line fails:
35 | static ref DATA: Option<&'static MyData> = load_data().ok().as_ref();
| ----------------^^^^^^^^^
| | returns a value referencing data owned by the current function
| |
| |
| temporary value created here
Is there a way to achieve what I want or am I conceptually wrong?

Resources