Is it a relative use-path?

Suppose we have the following src directory structure:

src
  |_ lib.rs
  |_ display.rs
  |_ style.rs

Suppose lib.rs contains the following:

mod style;
pub use style::{Style, Color};
mod display;
pub use display::*;

And now if display.rs contains the following code:

// Some code

#[cfg(test)]
mod tests {
    pub use style::Style;
}

It seems that the pub use style::Style refers to crate::Style or crate::style::Style (both are equivalent).
Why is it so? Aren't use paths not beginning with super, self or crate be relative paths in Rust 2018?

The same code as one file, for easier analysis (playground):

mod style {
    pub struct Style;
    pub struct Color;
}
pub use style::{Color, Style};

mod display {
    #[cfg(test)]
    mod tests {
        pub use style::Style;
    }
}
pub use display::*;

Not sure why is works wike this, however.

1 Like

The import with style::Style does not actually compile. It's just that it isn't compiled until you actually run the tests, so you don't get the error with cargo build.

Actually this code is a shortened version of the ansi_term crate, And running cargo test does actually show no failures.

Are there any use super::* lines in the area?

It could be that you get something like this after expanding the glob import:

use crate::style;
use style::Style;

Not in that module (there are globs at the crate root, but I don't think that matters).
For reference, this is all the code in that module:

#[cfg(test)]
mod tests {
    pub use super::super::ANSIStrings;
    pub use style::Style;
    pub use style::Colour::*;

    #[test]
    fn no_control_codes_for_plain() {
        let one = Style::default().paint("one");
        let two = Style::default().paint("two");
        let output = format!("{}", ANSIStrings( &[ one, two ] ));
        assert_eq!(&*output, "onetwo");
    }
}

It's because the ansi_term crate is not written in Rust 2018.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.