Skip to content

Commit c4db59f

Browse files
authored
Rollup merge of rust-lang#40452 - frewsxcv:frewsxcv-unstable-docs, r=GuillaumeGomez
Add basic documentation/examples for six unstable features.
2 parents bb1c465 + d3ae2eb commit c4db59f

File tree

7 files changed

+120
-1
lines changed

7 files changed

+120
-1
lines changed

src/doc/unstable-book/src/concat-idents.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ The tracking issue for this feature is: [#29599]
66

77
------------------------
88

9+
The `concat_idents` feature adds a macro for concatenating multiple identifiers
10+
into one identifier.
911

12+
## Examples
1013

14+
```rust
15+
#![feature(concat_idents)]
16+
17+
fn main() {
18+
fn foobar() -> u32 { 23 }
19+
let f = concat_idents!(foo, bar);
20+
assert_eq!(f(), 23);
21+
}
22+
```

src/doc/unstable-book/src/conservative-impl-trait.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,61 @@ The tracking issue for this feature is: [#34511]
66

77
------------------------
88

9+
The `conservative_impl_trait` feature allows a conservative form of abstract
10+
return types.
911

12+
Abstract return types allow a function to hide a concrete return type behind a
13+
trait interface similar to trait objects, while still generating the same
14+
statically dispatched code as with concrete types.
1015

16+
## Examples
17+
18+
```rust
19+
#![feature(conservative_impl_trait)]
20+
21+
fn even_iter() -> impl Iterator<Item=u32> {
22+
(0..).map(|n| n * 2)
23+
}
24+
25+
fn main() {
26+
let first_four_even_numbers = even_iter().take(4).collect::<Vec<_>>();
27+
assert_eq!(first_four_even_numbers, vec![0, 2, 4, 6]);
28+
}
29+
```
30+
31+
## Background
32+
33+
In today's Rust, you can write function signatures like:
34+
35+
````rust,ignore
36+
fn consume_iter_static<I: Iterator<u8>>(iter: I) { }
37+
38+
fn consume_iter_dynamic(iter: Box<Iterator<u8>>) { }
39+
````
40+
41+
In both cases, the function does not depend on the exact type of the argument.
42+
The type held is "abstract", and is assumed only to satisfy a trait bound.
43+
44+
* In the `_static` version using generics, each use of the function is
45+
specialized to a concrete, statically-known type, giving static dispatch,
46+
inline layout, and other performance wins.
47+
* In the `_dynamic` version using trait objects, the concrete argument type is
48+
only known at runtime using a vtable.
49+
50+
On the other hand, while you can write:
51+
52+
````rust,ignore
53+
fn produce_iter_dynamic() -> Box<Iterator<u8>> { }
54+
````
55+
56+
...but you _cannot_ write something like:
57+
58+
````rust,ignore
59+
fn produce_iter_static() -> Iterator<u8> { }
60+
````
61+
62+
That is, in today's Rust, abstract return types can only be written using trait
63+
objects, which can be a significant performance penalty. This RFC proposes
64+
"unboxed abstract types" as a way of achieving signatures like
65+
`produce_iter_static`. Like generics, unboxed abstract types guarantee static
66+
dispatch and inline data layout.

src/doc/unstable-book/src/const-fn.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,24 @@ The tracking issue for this feature is: [#24111]
66

77
------------------------
88

9+
The `const_fn` feature allows marking free functions and inherent methods as
10+
`const`, enabling them to be called in constants contexts, with constant
11+
arguments.
912

13+
## Examples
1014

15+
```rust
16+
#![feature(const_fn)]
17+
18+
const fn double(x: i32) -> i32 {
19+
x * 2
20+
}
21+
22+
const FIVE: i32 = 5;
23+
const TEN: i32 = double(FIVE);
24+
25+
fn main() {
26+
assert_eq!(5, FIVE);
27+
assert_eq!(10, TEN);
28+
}
29+
```

src/doc/unstable-book/src/const-indexing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ The tracking issue for this feature is: [#29947]
66

77
------------------------
88

9+
The `const_indexing` feature allows the constant evaluation of index operations
10+
on constant arrays and repeat expressions.
911

12+
## Examples
1013

14+
```rust
15+
#![feature(const_indexing)]
16+
17+
const ARR: [usize; 5] = [1, 2, 3, 4, 5];
18+
const ARR2: [usize; ARR[1]] = [42, 99];
19+
```

src/doc/unstable-book/src/i128-type.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,20 @@ The tracking issue for this feature is: [#35118]
66

77
------------------------
88

9+
The `i128_type` feature adds support for 128 bit signed and unsigned integer
10+
types.
911

12+
```rust
13+
#![feature(i128_type)]
14+
15+
fn main() {
16+
assert_eq!(1u128 + 1u128, 2u128);
17+
assert_eq!(u128::min_value(), 0);
18+
assert_eq!(u128::max_value(), 340282366920938463463374607431768211455);
19+
20+
assert_eq!(1i128 - 2i128, -1i128);
21+
assert_eq!(i128::min_value(), -170141183460469231731687303715884105728);
22+
assert_eq!(i128::max_value(), 170141183460469231731687303715884105727);
23+
}
24+
```
1025

src/doc/unstable-book/src/non-ascii-idents.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ The tracking issue for this feature is: [#28979]
66

77
------------------------
88

9+
The `non_ascii_idents` feature adds support for non-ASCII identifiers.
910

11+
## Examples
1012

13+
```rust
14+
#![feature(non_ascii_idents)]
15+
16+
const ε: f64 = 0.00001f64;
17+
const Π: f64 = 3.14f64;
18+
```

0 commit comments

Comments
 (0)