about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-08-23 12:32:15 +0200
committerGitHub <noreply@github.com>2024-08-23 12:32:15 +0200
commit26672c93d5417e9760190cf3777459332060a9c1 (patch)
tree4060fa6cd01440c763d3df17321f702d46d0da2e
parent724f612be8e2216a35d5ff5d193ba4508f892bf0 (diff)
parentdbad7581348d71822573e906c165bd08c366f196 (diff)
downloadrust-26672c93d5417e9760190cf3777459332060a9c1.tar.gz
rust-26672c93d5417e9760190cf3777459332060a9c1.zip
Rollup merge of #129276 - eduardosm:stabilize-char_indices_offset, r=Amanieu
Stabilize feature `char_indices_offset`

Stabilized API:

```rust
impl CharIndices<'_> {
    pub fn offset(&self) -> usize;
}
```

Tracking issue: https://github.com/rust-lang/rust/issues/83871

Closes https://github.com/rust-lang/rust/issues/83871

I also attempted to improved the documentation to make it more clear that it returns the offset of the character that will be returned by the next call to `next()`.
-rw-r--r--library/core/src/lib.rs1
-rw-r--r--library/core/src/str/iter.rs15
2 files changed, 13 insertions, 3 deletions
diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs
index e3640627c56..bbfe412fbcf 100644
--- a/library/core/src/lib.rs
+++ b/library/core/src/lib.rs
@@ -110,7 +110,6 @@
 #![cfg_attr(bootstrap, feature(offset_of_nested))]
 #![feature(array_ptr_get)]
 #![feature(asm_experimental_arch)]
-#![feature(char_indices_offset)]
 #![feature(const_align_of_val)]
 #![feature(const_align_of_val_raw)]
 #![feature(const_align_offset)]
diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs
index 06f796f9f3a..681ec79c0b7 100644
--- a/library/core/src/str/iter.rs
+++ b/library/core/src/str/iter.rs
@@ -241,24 +241,35 @@ impl<'a> CharIndices<'a> {
     /// Returns the byte position of the next character, or the length
     /// of the underlying string if there are no more characters.
     ///
+    /// This means that, when the iterator has not been fully consumed,
+    /// the returned value will match the index that will be returned
+    /// by the next call to [`next()`](Self::next).
+    ///
     /// # Examples
     ///
     /// ```
-    /// #![feature(char_indices_offset)]
     /// let mut chars = "a楽".char_indices();
     ///
+    /// // `next()` has not been called yet, so `offset()` returns the byte
+    /// // index of the first character of the string, which is always 0.
     /// assert_eq!(chars.offset(), 0);
+    /// // As expected, the first call to `next()` also returns 0 as index.
     /// assert_eq!(chars.next(), Some((0, 'a')));
     ///
+    /// // `next()` has been called once, so `offset()` returns the byte index
+    /// // of the second character ...
     /// assert_eq!(chars.offset(), 1);
+    /// // ... which matches the index returned by the next call to `next()`.
     /// assert_eq!(chars.next(), Some((1, '楽')));
     ///
+    /// // Once the iterator has been consumed, `offset()` returns the length
+    /// // in bytes of the string.
     /// assert_eq!(chars.offset(), 4);
     /// assert_eq!(chars.next(), None);
     /// ```
     #[inline]
     #[must_use]
-    #[unstable(feature = "char_indices_offset", issue = "83871")]
+    #[stable(feature = "char_indices_offset", since = "CURRENT_RUSTC_VERSION")]
     pub fn offset(&self) -> usize {
         self.front_offset
     }