Search code examples
rustself

What is the meaning of `use path::{self}`?


I recently read the following line of code:

use fmt::{self, Debug}; 

What does the self keyword in the above statement mean?


Solution

  • Using self in that context allows you to bind a module plus some of it's child elements into the current scope with a single use statement.

    Without self:

    use a::b::{c,d};
    // Now you can refer to a::b::c as c and a::b::d as d
    // but if you need to refer to a::b as a::b
    

    With self:

    use a::b::{self, c, d};
    // Now b refers to a::b as well