I have a lot of re-exports across my code for convenience, but sometimes I’d like to specifically exclude something to avoid a possible naming confusion or conflicting implementation.
Coming from Scala, I would expect the below to NOT compile (I don’t want it to), because the Bar as _ syntax would drop Bar from the import. However, this still compiles. Is there some rust syntax that does what I want?
pub mod stuff{
pub struct Foo{}
pub struct Bar{}
}
pub mod other_stuff{
use super::stuff::{*, Bar as _};
pub struct Baz{
foo : Foo,
bar : Bar
}
}
To be clear, I want to use everything from stuff EXCEPT for Bar. (obvs in this case I could just import Foo, but my actual use cases have lots of imports and I don’t want to itemize all-but-one of them.)
EDIT: I’d still like to know how to do this, but I realize I’d made an odd mistake in my motivating example. Posting here for context and in case anyone else is tripped on the same thing. Basically, what I actually had looked like:
pub mod stuff{
pub struct Foo;
pub struct Bar;
}
pub mod yet_more_stuff{
use super::stuff::*;
pub struct Bar{}
pub struct Baz{
pub foo : Foo,
pub bar : Bar
}
impl Baz{
pub fn new() -> Self{
Self{
foo : Foo,
bar : Bar
}
}
}
}
The above code block does NOT compile; because I defined Bar in one place as struct Bar; and in another as struct Bar{}, the type in Baz was yet_more_stuff:Bar but the value constructed in Baz::new() was stuff::Bar. This looked like it was conflicting rather than taking precedence as the answer below describes.
>Solution :
There is no syntax in Rust for that.
Do notice, however, that specific imports always take precedence over glob imports, so if you have use stuff::* and use other_stuff::Bar where stuff also contains Bar, there will be no conflict and the Bar from other_stuff will always be chosen. There will only be a conflict between two glob imports or two specific imports.
In general, however, glob imports are discouraged and it’s better to use specific imports, especially if you don’t want to import everything.