3 A *path* is a sequence of one or more path segments _logically_ separated by
4 a namespace <span class="parenthetical">qualifier (`::`)</span>. If a path
5 consists of only one segment, it refers to either an [item] or a [variable] in
6 a local control scope. If a path has multiple segments, it always refers to an
9 Two examples of simple paths consisting of only identifier segments:
11 <!-- ignore: syntax fragment -->
21 > **<sup>Syntax</sup>**\
23 > `::`<sup>?</sup> _SimplePathSegment_ (`::` _SimplePathSegment_)<sup>\*</sup>
25 > _SimplePathSegment_ :\
26 > [IDENTIFIER] | `super` | `self` | `crate` | `$crate`
28 Simple paths are used in [visibility] markers, [attributes], [macros], and [`use`] items.
32 use std::io::{self, Write};
34 #[clippy::cyclomatic_complexity = "0"]
35 pub (in super) fn f1() {}
39 ### Paths in expressions
41 > **<sup>Syntax</sup>**\
42 > _PathInExpression_ :\
43 > `::`<sup>?</sup> _PathExprSegment_ (`::` _PathExprSegment_)<sup>\*</sup>
45 > _PathExprSegment_ :\
46 > _PathIdentSegment_ (`::` _GenericArgs_)<sup>?</sup>
48 > _PathIdentSegment_ :\
49 > [IDENTIFIER] | `super` | `self` | `Self` | `crate` | `$crate`
52 > `<` `>`\
53 > | `<` ( _GenericArg_ `,` )<sup>\*</sup> _GenericArg_ `,`<sup>?</sup> `>`
56 > [_Lifetime_] | [_Type_] | _GenericArgsConst_ | _GenericArgsBinding_
58 > _GenericArgsConst_ :\
59 > [_BlockExpression_]\
60 > | [_LiteralExpression_]\
61 > | `-` [_LiteralExpression_]\
62 > | [_SimplePathSegment_]
64 > _GenericArgsBinding_ :\
65 > [IDENTIFIER] `=` [_Type_]
67 Paths in expressions allow for paths with generic arguments to be specified. They are
68 used in various places in [expressions] and [patterns].
70 The `::` token is required before the opening `<` for generic arguments to avoid
71 ambiguity with the less-than operator. This is colloquially known as "turbofish" syntax.
74 (0..10).collect::<Vec<_>>();
75 Vec::<u8>::with_capacity(1024);
78 The order of generic arguments is restricted to lifetime arguments, then type
79 arguments, then const arguments, then equality constraints.
81 Const arguments must be surrounded by braces unless they are a
82 [literal] or a single segment path.
86 > **<sup>Syntax</sup>**\
87 > _QualifiedPathInExpression_ :\
88 > _QualifiedPathType_ (`::` _PathExprSegment_)<sup>+</sup>
90 > _QualifiedPathType_ :\
91 > `<` [_Type_] (`as` _TypePath_)? `>`
93 > _QualifiedPathInType_ :\
94 > _QualifiedPathType_ (`::` _TypePathSegment_)<sup>+</sup>
96 Fully qualified paths allow for disambiguating the path for [trait implementations] and
97 for specifying [canonical paths](#canonical-paths). When used in a type specification, it
98 supports using the type syntax specified below.
103 fn f() { println!("S"); }
106 fn f() { println!("T1 f"); }
110 fn f() { println!("T2 f"); }
113 S::f(); // Calls the inherent impl.
114 <S as T1>::f(); // Calls the T1 trait function.
115 <S as T2>::f(); // Calls the T2 trait function.
120 > **<sup>Syntax</sup>**\
122 > `::`<sup>?</sup> _TypePathSegment_ (`::` _TypePathSegment_)<sup>\*</sup>
124 > _TypePathSegment_ :\
125 > _PathIdentSegment_ `::`<sup>?</sup> ([_GenericArgs_] | _TypePathFn_)<sup>?</sup>
128 > `(` _TypePathFnInputs_<sup>?</sup> `)` (`->` [_Type_])<sup>?</sup>
130 > _TypePathFnInputs_ :\
131 > [_Type_] (`,` [_Type_])<sup>\*</sup> `,`<sup>?</sup>
133 Type paths are used within type definitions, trait bounds, type parameter bounds,
136 Although the `::` token is allowed before the generics arguments, it is not required
137 because there is no ambiguity like there is in _PathInExpression_.
141 # pub struct Range<T> {f1: T}
142 # pub trait Index<T> {}
143 # pub struct Example<'a> {f1: &'a i32}
146 impl ops::Index<ops::Range<usize>> for S { /*...*/ }
147 fn i<'a>() -> impl Iterator<Item = ops::Example<'a>> {
149 # const EXAMPLE: Vec<ops::Example<'static>> = Vec::new();
150 # EXAMPLE.into_iter()
152 type G = std::boxed::Box<dyn std::ops::FnOnce(isize) -> isize>;
157 Paths can be denoted with various leading qualifiers to change the meaning of
162 Paths starting with `::` are considered to be *global paths* where the segments of the path
163 start being resolved from a place which differs based on edition. Each identifier in
164 the path must resolve to an item.
166 > **Edition Differences**: In the 2015 Edition, identifiers resolve from the "crate root"
167 > (`crate::` in the 2018 edition), which contains a variety of different items, including
168 > external crates, default crates such as `std` or `core`, and items in the top level of
169 > the crate (including `use` imports).
171 > Beginning with the 2018 Edition, paths starting with `::` resolve from
172 > crates in the [extern prelude]. That is, they must be followed by the name of a crate.
180 ::a::foo(); // call `a`'s foo function
181 // In Rust 2018, `::a` would be interpreted as the crate `a`.
189 `self` resolves the path relative to the current module. `self` can only be used as the
190 first segment, without a preceding `::`.
202 `Self`, with a capital "S", is used to refer to the implementing type within
203 [traits] and [implementations].
205 `Self` can only be used as the first segment, without a preceding `::`.
211 // `Self` will be whatever type that implements `T`.
213 // `Self::Item` will be the type alias in the implementation.
214 fn f(&self) -> Self::Item;
220 fn new() -> Self { // `Self` is the type `S`.
223 fn f(&self) -> Self::Item { // `Self::Item` is the type `i32`.
224 Self::C // `Self::C` is the constant value `9`.
231 `super` in a path resolves to the parent module. It may only be used in leading
232 segments of the path, possibly after an initial `self` segment.
240 super::a::foo(); // call a's foo function
246 `super` may be repeated several times after the first `super` or `self` to refer to
256 super::super::foo(); // call a's foo function
257 self::super::super::foo(); // call a's foo function
267 `crate` resolves the path relative to the current crate. `crate` can only be used as the
268 first segment, without a preceding `::`.
282 `$crate` is only used within [macro transcribers], and can only be used as the first
283 segment, without a preceding `::`. `$crate` will expand to a path to access items from the
284 top level of the crate where the macro is defined, regardless of which crate the macro is
288 pub fn increment(x: u32) -> u32 {
294 ($x:expr) => ( $crate::increment($x) )
301 Items defined in a module or implementation have a *canonical path* that
302 corresponds to where within its crate it is defined. All other paths to these
303 items are aliases. The canonical path is defined as a *path prefix* appended by
304 the path segment the item itself defines.
306 [Implementations] and [use declarations] do not have canonical paths, although
307 the items that implementations define do have them. Items defined in
308 block expressions do not have canonical paths. Items defined in a module that
309 does not have a canonical path do not have a canonical path. Associated items
310 defined in an implementation that refers to an item without a canonical path,
311 e.g. as the implementing type, the trait being implemented, a type parameter or
312 bound on a type parameter, do not have canonical paths.
314 The path prefix for modules is the canonical path to that module. For bare
315 implementations, it is the canonical path of the item being implemented
316 surrounded by <span class="parenthetical">angle (`<>`)</span> brackets. For
317 [trait implementations], it is the canonical path of the item being implemented
318 followed by `as` followed by the canonical path to the trait all surrounded in
319 <span class="parenthetical">angle (`<>`)</span> brackets.
321 The canonical path is only meaningful within a given crate. There is no global
322 namespace across crates; an item's canonical path merely identifies it within
326 // Comments show the canonical path of the item.
329 pub struct Struct; // ::a::Struct
331 pub trait Trait { // ::a::Trait
332 fn f(&self); // ::a::Trait::f
335 impl Trait for Struct {
336 fn f(&self) {} // <::a::Struct as ::a::Trait>::f
340 fn g(&self) {} // <::a::Struct>::g
344 mod without { // ::without
345 fn canonicals() { // ::without::canonicals
346 struct OtherStruct; // None
348 trait OtherTrait { // None
352 impl OtherTrait for OtherStruct {
353 fn g(&self) {} // None
356 impl OtherTrait for ::a::Struct {
357 fn g(&self) {} // None
360 impl ::a::Trait for OtherStruct {
361 fn f(&self) {} // None
369 [_BlockExpression_]: expressions/block-expr.md
370 [_Expression_]: expressions.md
371 [_GenericArgs_]: #paths-in-expressions
372 [_Lifetime_]: trait-bounds.md
373 [_LiteralExpression_]: expressions/literal-expr.md
374 [_SimplePathSegment_]: #simple-paths
375 [_Type_]: types.md#type-expressions
376 [literal]: expressions/literal-expr.md
378 [variable]: variables.md
379 [implementations]: items/implementations.md
380 [use declarations]: items/use-declarations.md
381 [IDENTIFIER]: identifiers.md
382 [`use`]: items/use-declarations.md
383 [attributes]: attributes.md
384 [expressions]: expressions.md
385 [extern prelude]: names/preludes.md#extern-prelude
386 [macro transcribers]: macros-by-example.md
387 [macros]: macros-by-example.md
388 [patterns]: patterns.md
389 [trait implementations]: items/implementations.md#trait-implementations
390 [traits]: items/traits.md
391 [visibility]: visibility-and-privacy.md