Skip to content

Commit c83afb9

Browse files
committed
doc: Document flavorful variations of paths
Closes #4293
1 parent bc234ae commit c83afb9

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/doc/rust.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ operators](#binary-operator-expressions), or [keywords](#keywords).
432432
## Paths
433433

434434
~~~~ {.notrust .ebnf .gram}
435-
expr_path : ident [ "::" expr_path_tail ] + ;
435+
expr_path : [ "::" ] ident [ "::" expr_path_tail ] + ;
436436
expr_path_tail : '<' type_expr [ ',' type_expr ] + '>'
437437
| expr_path ;
438438
@@ -475,6 +475,51 @@ let x = id::<int>(10); // Type arguments used in a call expression
475475
# }
476476
~~~~
477477

478+
Paths can be denoted with various leading qualifiers to change the meaning of
479+
how it is resolved:
480+
481+
* Paths starting with `::` are considered to be global paths where the
482+
components of the path start being resolved from the crate root. Each
483+
identifier in the path must resolve to an item.
484+
485+
```rust
486+
mod a {
487+
pub fn foo() {}
488+
}
489+
mod b {
490+
pub fn foo() {
491+
::a::foo(); // call a's foo function
492+
}
493+
}
494+
# fn main() {}
495+
```
496+
497+
* Paths starting with the keyword `super` begin resolution relative to the
498+
parent module. Each further identifier must resolve to an item
499+
500+
```rust
501+
mod a {
502+
pub fn foo() {}
503+
}
504+
mod b {
505+
pub fn foo() {
506+
super::a::foo(); // call a's foo function
507+
}
508+
}
509+
# fn main() {}
510+
```
511+
512+
* Paths starting with the keyword `self` begin resolution relative to the
513+
current module. Each further identifier must resolve to an item.
514+
515+
```rust
516+
fn foo() {}
517+
fn bar() {
518+
self::foo();
519+
}
520+
# fn main() {}
521+
```
522+
478523
# Syntax extensions
479524

480525
A number of minor features of Rust are not central enough to have their own

0 commit comments

Comments
 (0)