These are chat archives for rust-lang/rust

28th
Jan 2019
thojest
@thojest
Jan 28 08:38
short question :)
in some code i found the following expression pub use crate::Foo;
what exactly is it doing?
sry i mean pub use crate::foo::Foo
thojest
@thojest
Jan 28 08:44
foo seems to be a module and Foo a struct inside foo
Tim Robinson
@1tgr
Jan 28 09:18
It’s saying that Foo is exposed publicly in your module as if it was defined there
Whereas really the definition is in module crate::foo, in a different source file
It’s a common way to expose a simplified API from your crate (a facade) while keeping the ability to put the code in multiple source files internally
thojest
@thojest
Jan 28 09:20
hmm
but foo is a module I think
e.g.
pub mod exit;
pub use crate::exit::Exit;
Tim Robinson
@1tgr
Jan 28 09:21
Exactly
You have a file exit.rs, inside that is pub struct Exit
thojest
@thojest
Jan 28 09:22
aahh ok
Tim Robinson
@1tgr
Jan 28 09:22
This code is saying that your crate exposes a module exit, with a type inside called Exit
thojest
@thojest
Jan 28 09:22
i think my real question is
Tim Robinson
@1tgr
Jan 28 09:22
And the top level of your crate also exposes the same type
thojest
@thojest
Jan 28 09:22
what is the difference between
pub use crate::exit::Exit and pub use self::exit::Exit
Tim Robinson
@1tgr
Jan 28 09:24
Within lib.rs they are identical
thojest
@thojest
Jan 28 09:24
ahh ok
Tim Robinson
@1tgr
Jan 28 09:24
Within lib.rs, crate is the same as self
thojest
@thojest
Jan 28 09:24
thanks a lot :)
Zakarum
@omni-viral
Jan 28 09:31
@vlopes11 for your specific case I would avoid specifying trait bounds in type definition
Ogulcan Erduran
@erduranogulcan
Jan 28 11:45
I can access to other rs files which are in src folder. Using "mod fileinsrc;" but when I create a folder in src and 2 files in that folder, I can not access one file to other file in folder like file2insubfolder.rs : "mod file1insubfolder;" Mod is not working in sub folder's file. How can I access to other file in sub folder?
Zakarum
@omni-viral
Jan 28 12:44
This message was deleted
@erduranogulcan It is not like that
mod <filename> can be used in lib.rs (or main.rs for executables) to add <filename> from src directory to the module hierarhy
Also you can create a directory <dirname>
There you should add mod.rs file.
Now if you add mod <dirname> to the lib.rs you will add <dirname>/mod.rs file to the source tree
And in <dirname>/mod.rs you can add source files from <dirname> using mod <filename> the same way as you do with lib.rs in src directory
Here is simple example.
Files:
lib.rs
foo\mod.rs
foo\bar.rs
in lib.rs:
mod foo;
Zakarum
@omni-viral
Jan 28 12:49
in foo/mod.rs
mod bar;
Alternatively to <dirname>/mod.rs you can have <dirname>.rs file. Those names will result in same behaviour

So you can have

foo.rs
foo/bar.rs

and add foo/bar.rs to the source tree with mod bar; in foo.rs

Zakarum
@omni-viral
Jan 28 12:55
Now to import symbols in sibling files <dirname>/foo.rs and <dirname>/bar.rs you should write use statement.
use super::foo in bar.rs will import foo module. super refers to the parent module. i.e. one level higher that self (self refers to current module, yes, it is confusing as the same keyword is used in methods), so super::foo referens to the sibling module foo.
Instead of use statement you can refer to the symbols from foo directly as super::foo::SomethingFromFoo
Also you can refer to foo from creates root as crate::full::path::to::foo
Ogulcan Erduran
@erduranogulcan
Jan 28 17:49
@omni-viral thank you so much for explanation.