Skip to content

Commit

Permalink
Rollup merge of #70588 - Coder-256:str-split-at-docs, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Fix incorrect documentation for `str::{split_at, split_at_mut}`

The documentation for each method currently states:

> Panics if `mid` is not on a UTF-8 code point boundary, or if it is beyond the last code point of the string slice.

However, this is not consistent with the real behavior, or that of the corresponding methods for `[T]` slices. A comment inside each of the `str` methods states:

> is_char_boundary checks that the index is in [0, .len()]

That is what I would expect the behavior to be, and in fact this seems to be the real behavior. For example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8e03dcc209d4dd176df2297523f9fee1)):

```rust
fn main() {
    // Prints ("abc", "") and doesn't panic
    println!("{:?}", "abc".split_at(3));
}
```

In this case, I would interpret "the last code point of the string slice" to mean the byte at index 2 in UTF-8. However, it is possible to pass an index of 3, which is definitely "beyond the last code point of the string slice".

I think that this is much clearer, but feel free to bikeshed.
  • Loading branch information
Centril authored Mar 31, 2020
2 parents 9d25ca5 + fcab1f9 commit bf7870d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,7 @@ impl str {
/// # Panics
///
/// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
/// beyond the last code point of the string slice.
/// past the end of the last code point of the string slice.
///
/// # Examples
///
Expand Down Expand Up @@ -2683,7 +2683,7 @@ impl str {
/// # Panics
///
/// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
/// beyond the last code point of the string slice.
/// past the end of the last code point of the string slice.
///
/// # Examples
///
Expand Down

0 comments on commit bf7870d

Please sign in to comment.