Skip to content

Commit f2a9a5e

Browse files
committed
Merge branch 'full-range-syntax' of https://github.com/bluss/rfcs into bluss-full-range-syntax
2 parents 621fcaf + 25d35f1 commit f2a9a5e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ the direction the language is evolving in.
7171
* [0546-Self-not-sized-by-default.md](text/0546-Self-not-sized-by-default.md)
7272
* [0550-macro-future-proofing.md](text/0550-macro-future-proofing.md)
7373
* [0587-fn-return-should-be-an-associated-type.md](text/0587-fn-return-should-be-an-associated-type.md)
74+
* [0702-rangefull-expression.md](text/0702-rangefull-expression.md)
7475

7576
## Table of Contents
7677
[Table of Contents]: #table-of-contents

text/0702-rangefull-expression.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
- Start Date: 2015-01-21
2+
- RFC PR: [#702](https://github.com/rust-lang/rfcs/pull/702)
3+
- Rust Issue: [#21879](https://github.com/rust-lang/rust/issues/21879)
4+
5+
# Summary
6+
7+
Add the syntax `..` for `std::ops::RangeFull`.
8+
9+
# Motivation
10+
11+
Range expressions `a..b`, `a..` and `..b` all have dedicated syntax and
12+
produce first-class values. This means that they will be usable and
13+
useful in custom APIs, so for consistency, the fourth slicing range,
14+
`RangeFull`, could have its own syntax `..`
15+
16+
# Detailed design
17+
18+
`..` will produce a `std::ops::RangeFull` value when it is used in an
19+
expression. This means that slicing the whole range of a sliceable
20+
container is written `&foo[..]`.
21+
22+
We should remove the old `&foo[]` syntax for consistency. Because of
23+
this breaking change, it would be best to change this before Rust 1.0.
24+
25+
As previously stated, when we have range expressions in the language,
26+
they become convenient to use when stating ranges in an API.
27+
28+
@Gankro fielded ideas where
29+
methods like for example `.remove(index) -> element` on a collection
30+
could be generalized by accepting either indices or ranges. Today's `.drain()`
31+
could be expressed as `.remove(..)`.
32+
33+
Matrix or multidimensional array APIs can use the range expressions for
34+
indexing and/or generalized slicing and `..` represents selecting a full axis
35+
in a multidimensional slice, i.e. `(1..3, ..)` slices the first axis and
36+
preserves the second.
37+
38+
Because of deref coercions, the very common conversions of String or Vec to
39+
slices don't need to use slicing syntax at all, so the change in verbosity from
40+
`[]` to `[..]` is not a concern.
41+
42+
# Drawbacks
43+
44+
* Removing the slicing syntax `&foo[]` is a breaking change.
45+
46+
* `..` already appears in patterns, as in this example:
47+
`if let Some(..) = foo { }`. This is not a conflict per se, but the
48+
same syntax element is used in two different ways in Rust.
49+
50+
# Alternatives
51+
52+
* We could add this syntax later, but we would end up with duplicate
53+
slicing functionality using `&foo[]` and `&foo[..]`.
54+
55+
* `0..` could replace `..` in many use cases (but not for ranges in
56+
ordered maps).
57+
58+
# Unresolved questions
59+
60+
Any parsing questions should already be mostly solved because of the
61+
`a..` and `..b` cases.

0 commit comments

Comments
 (0)