- Start Date: 2014-12-03
- RFC PR: rust-lang/rfcs#495
- Rust Issue: rust-lang/rust#23121
Summary
Change array/slice patterns in the following ways:
- Make them only match一致する、マッチさせるon arrays配列(
[T; n]
and[T]
), not slices; - Make subslice matching一致する、マッチさせるyield産出する、出力するa value of type
[T; n]
or[T]
, not&[T]
or&mut [T]
; - Allow許可する、可能にするmultiple複数のmutable references参照to be made to different parts of the same array配列or slice in array配列patterns (resolving rust-lang/rust issue #8636).
Motivation
Before DST (and after the removal of ~[T]
), there were only two types based[T]
: &[T]
and &mut [T]
. With DST, we can have many more types based[T]
, Box<[T]>
in particular, but theoretically any pointer type around a [T]
could be used. However, array&[T]
, &mut [T]
, and [T; n]
only, meaning that to matchBox<[T]>
, one must first convert
Another problem with today’s array
This makes sense, but still has a few problems. In particular, tail
is a &[int]
, even though the compiler can always assert that it will have a length of 2
, so there is no way to treatref
). This can create confusing errors because of the fact that the ..
syntaxref
keyword.
Finally, the compiler currently complains when one tries to take
This fails to compile, because the compiler thinks that this would allow
Detailed design設計(する)
-
Make array
配列patterns match一致する、マッチさせるonly on arrays配列([T; n]
and[T]
). For example, the following code:Would have to be changed to this:
This change makes slice patterns mirror slice expressions
式much more closely. -
Make subslice matching
一致する、マッチさせるin array配列patterns yield産出する、出力するa value of type[T; n]
(if the array配列is of fixed size) or[T]
(if not). This means changing most code that looks like this:To this:
It should be noted that if a fixed-size array
配列is matched一致する、マッチさせるon using subslice matching,一致する、マッチさせるandref
is used, the type of the binding will be&[T; n]
, not&[T]
. -
Improve the compiler’s analysis
解析of multiple複数のmutable references参照to the same value within array配列patterns. This would be done by allowing許可する、可能にするmultiple複数のmutable references参照to different elements要素of the same array配列(including bindings from subslice matching):
Drawbacks
-
This will break a non-negligible amount of code, requiring people to add
&
s andref
s to their code. -
The modifications to subslice matching
一致する、マッチさせるwill requireref
orref mut
to be used in almost all cases. This could be seen as unnecessary.
Alternatives代わりのもの、選択肢
- Do a subset部分集合of this proposal; for example, the modifications to subslice matching一致する、マッチさせるin patterns could be removed.
Unresolved questions
- What are the precise implications to the borrow checker of the change to multiple複数のmutable borrows in the same array配列pattern? Since it is a backwards-compatible change, it can be implemented実装するafter 1.0 if it turns out to be difficult to implement.実装する