- Feature Name:
panic_safe_slicing
- Start Date: 2015-10-16
- RFC PR: rust-lang/rfcs#1679
- Rust Issue: rust-lang/rfcs#35729
Summary
Add "panic-safe" or "total" alternatives
Motivation
SliceExt::get
and SliceExt::get_mut
can be thought as non-panicking versions of the simple indexing syntax,a[idx]
, and SliceExt::get_unchecked
and SliceExt::get_unchecked_mut
can be thought of as unsafe versions with boundsa[start..end]
, a[start..]
, or a[..end]
. This RFC proposes such methods to fill the gap.
Detailed design設計(する)
The get
, get_mut
, get_unchecked
, and get_unchecked_mut
will be made generic over usize
as well as rangesusize
like slice's Index
implementationa.get(start..end)
which will behave analagously to a[start..end]
.
Because methods cannot be overloaded in an ad-hoc manner in the same way that traits may be implemented,SliceIndex
trait which is implemented
And then alter the Index
, IndexMut
, get
, get_mut
, get_unchecked
, and get_mut_unchecked
implementationsSliceIndex
:
Drawbacks
- The
SliceIndex
trait is unfortunate - it's tuned for exactly正確にthe setセットする、集合of methods it's used by. It only exists because inherent methods cannot be overloaded the same way that trait implementations実装can be. It would most likely remain unstable indefinitely. - Documentation文書may suffer. Rustdoc output currently explicitly明示的にshows each of the ways you can index a slice, while there will simply be a single単一のgeneric implementation実装with this change. This may not be that bad, though. The doc block currently seems to provided the most valuable information to newcomers rather than the trait bound,制限する、結び付けられてand that will still be presentあるwith this change.
Alternatives代わりのもの、選択肢
- Stay as is.
- A previous前のversion of this RFC introduced new
get_slice
etc methods rather than overloadingget
etc. This avoids避ける、回避するthe utility trait but is somewhat less ergonomic. - Instead of one trait amalgamating all of the required methods, we could have one trait per method. This would open a more reasonable door to stabilizing those traits, but adds quite a lot more surface area. Replacing an unstable
SliceIndex
trait with a collection would be backwards compatible.
Unresolved questions
None