Skip to content

Commit ef2b131

Browse files
committed
Add docs for crate_in_paths, extern_in_paths and extern_absolute_paths into the unstable book
1 parent 32db83b commit ef2b131

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# `crate_in_paths`
2+
3+
The tracking issue for this feature is: [#44660]
4+
5+
[#44660]: https://github.com/rust-lang/rust/issues/44660
6+
7+
------------------------
8+
9+
The `crate_in_paths` feature allows to explicitly refer to the crate root in absolute paths
10+
using keyword `crate`.
11+
12+
`crate` can be used *only* in absolute paths, i.e. either in `::crate::a::b::c` form or in `use`
13+
items where the starting `::` is added implicitly.
14+
Paths like `crate::a::b::c` are not accepted currently.
15+
16+
This feature is required in `feature(extern_absolute_paths)` mode to refer to any absolute path
17+
in the local crate (absolute paths refer to extern crates by default in that mode), but can be
18+
used without `feature(extern_absolute_paths)` as well.
19+
20+
```rust
21+
#![feature(crate_in_paths)]
22+
23+
// Imports, `::` is added implicitly
24+
use crate::m::f;
25+
use crate as root;
26+
27+
mod m {
28+
pub fn f() -> u8 { 1 }
29+
pub fn g() -> u8 { 2 }
30+
pub fn h() -> u8 { 3 }
31+
32+
// OK, visibilities implicitly add starting `::` as well, like imports
33+
pub(in crate::m) struct S;
34+
}
35+
36+
mod n
37+
{
38+
use crate::m::f;
39+
use crate as root;
40+
pub fn check() {
41+
assert_eq!(f(), 1);
42+
// `::` is required in non-import paths
43+
assert_eq!(::crate::m::g(), 2);
44+
assert_eq!(root::m::h(), 3);
45+
}
46+
}
47+
48+
fn main() {
49+
assert_eq!(f(), 1);
50+
assert_eq!(::crate::m::g(), 2);
51+
assert_eq!(root::m::h(), 3);
52+
n::check();
53+
}
54+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# `extern_absolute_paths`
2+
3+
The tracking issue for this feature is: [#44660]
4+
5+
[#44660]: https://github.com/rust-lang/rust/issues/44660
6+
7+
------------------------
8+
9+
The `extern_absolute_paths` feature enables mode allowing to refer to names from other crates
10+
"inline", without introducing `extern crate` items, using absolute paths like `::my_crate::a::b`.
11+
12+
`::my_crate::a::b` will resolve to path `a::b` in crate `my_crate`.
13+
14+
`feature(crate_in_paths)` can be used in `feature(extern_absolute_paths)` mode for referring
15+
to absolute paths in the local crate (`::crate::a::b`).
16+
17+
`feature(extern_in_paths)` provides the same effect by using keyword `extern` to refer to
18+
paths from other crates (`extern::my_crate::a::b`).
19+
20+
```rust,ignore
21+
#![feature(extern_absolute_paths)]
22+
23+
// Suppose we have a dependency crate `xcrate` available through `Cargo.toml`, or `--extern`
24+
// options, or standard Rust distribution, or some other means.
25+
26+
use xcrate::Z;
27+
28+
fn f() {
29+
use xcrate;
30+
use xcrate as ycrate;
31+
let s = xcrate::S;
32+
assert_eq!(format!("{:?}", s), "S");
33+
let z = ycrate::Z;
34+
assert_eq!(format!("{:?}", z), "Z");
35+
}
36+
37+
fn main() {
38+
let s = ::xcrate::S;
39+
assert_eq!(format!("{:?}", s), "S");
40+
let z = Z;
41+
assert_eq!(format!("{:?}", z), "Z");
42+
}
43+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# `extern_in_paths`
2+
3+
The tracking issue for this feature is: [#44660]
4+
5+
[#44660]: https://github.com/rust-lang/rust/issues/44660
6+
7+
------------------------
8+
9+
The `extern_in_paths` feature allows to refer to names from other crates "inline", without
10+
introducing `extern crate` items, using keyword `extern`.
11+
12+
For example, `extern::my_crat::a::b` will resolve to path `a::b` in crate `my_crate`.
13+
14+
`feature(extern_absolute_paths)` mode provides the same effect by resolving absolute paths like
15+
`::my_crate::a::b` to paths from extern crates by default.
16+
17+
```rust,ignore
18+
#![feature(extern_in_paths)]
19+
20+
// Suppose we have a dependency crate `xcrate` available through `Cargo.toml`, or `--extern`
21+
// options, or standard Rust distribution, or some other means.
22+
23+
use extern::xcrate::Z;
24+
25+
fn f() {
26+
use extern::xcrate;
27+
use extern::xcrate as ycrate;
28+
let s = xcrate::S;
29+
assert_eq!(format!("{:?}", s), "S");
30+
let z = ycrate::Z;
31+
assert_eq!(format!("{:?}", z), "Z");
32+
}
33+
34+
fn main() {
35+
let s = extern::xcrate::S;
36+
assert_eq!(format!("{:?}", s), "S");
37+
let z = Z;
38+
assert_eq!(format!("{:?}", z), "Z");
39+
}
40+
```

0 commit comments

Comments
 (0)