Skip to content

Prefer super or crate for internal module references #167

@tisonkun

Description

@tisonkun

We can refer to modules in the same crate in different ways. For example, given a crate like this:

src/
  lib.rs
  a.rs
  b.rs

a can import trait B in b as crate::b::B or super::b::B. And if B is pub use in lib.rs, a can also refer B as crate::B. All these work, but it is troublesome to think about which way to go every time. So I think it will be better if we have a guideline about this.

My opinion is that we should prefer referring things relatively using super::b::B because:

  • It doesn't assume if B is public or not
  • When refactoring file structure, for example, if we want to move a.rs and b.rs to a sub-directory, the code still works

Originally posted by @huachaohuang in rust-lang/api-guidelines#255

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions