Rationale for relative vs absolute paths being different in use statements?


#1

I’m a little confused why use statements in operate on absolute paths, whereas everything else uses relative paths. This is particularly noticeable when nesting modules…

mod foo {
    mod bar {
        use std::mem; // use can refer to crates using absolute paths without ::

        fn func() -> usize {
            ::std::mem::size_of(f32); // but here we have to explicitly specify absolute path with ::
        }
    }
}

What is the rationale for these two cases having subtly different behaviour?


#2

This is super confusing as you observed. It is going to be fixed. Check out RFC 2126 for the accepted design proposal and rust-lang/rust#44660 for the tracking issue that records progress toward supporting the improved approach in the compiler.


#3

use statements currently default to absolute paths “because” they tend to be used to import things from dependencies or across the module hierarchy, so this is a useful shortcut.

They do not include the ability to reference relative names because this is very challenging and confusing to implement correctly.