#Cross-file

12 messages · Page 1 of 1 (latest)

spiral spear
#

My first question, how do these three files relate to a main.rs or lib.rs? Are they all direct children modules or is there more structure to them?

#

Rust doesn't use imports as other languages have them. You need to put all your files into a module tree then use "paths" to indicate which item from with module you want in that tree

#

-modules

rugged hollowBOT
spiral spear
#

This diagram shows the basic idea of the module tree

terse gyro
#

instead of thinking about how to use functions and structs (idk what you mean by "variables") from another file, think about how you want your modules to be laid out and the file structure will follow.

Lets say we want a module structure like this

main
| one
| two
| | three

where main is our crate root, it has 2 submodules one and two, and two has a submodule of three. In this example our filestructure would look like this

src/
    main.rs
    one.rs
    two.rs
    two/
        three.rs

and the files would look something like this

// src/main.rs
pub mod one;
pub mod two;

use one::OneStruct;
// src/one.rs;
use crate::two::TwoStruct;
use crate::two::three::ThreeStruct;

pub struct OneStruct {
    field: TwoStruct,
    other_field: ThreeStruct,
    // ...
}
// src/two.rs
pub mod three;

pub struct TwoStruct { ... }
// src/two/three.rs
pub struct ThreeStruct { ... }
spiral spear
#

So taking what @terse gyro has shown and applying it to your example, we would have a lib.rs with ```rs
mod one;
mod two;
mod three;

and then anywhere you would be able to use `crate::two ::MyThing` to refer to an item in those modules
#

(ignore the extra space, discord is being stupid with emojis)

terse gyro
#

what do you mean "Value variable"? Is there some let Value thing in one function you are trying to use from another function? Because that doesn't work

#

variables only exist within their own scope. It doesn't make sense to try to "use" a variable from one function in another since that variable doesn't exist at that point

#

python works a bit differently to rust, and I would argue that you shouldn't even try to do that in python

#

instead you should structure your program in a way where you handle and pass data through functions