Skip to content

Commit 27b1f09

Browse files
committed
Rollup merge of #32099 - bluss:doc-string-slicing, r=alexcrichton
Clarify documentation for string slicing (Index impls) Clarify documentation for string slicing (Index impls) - Mention the sugared syntax for the implementations, since it's not apparent from the docs that `Index<Range<usize>>` corresponds to `&self[a..b]`. - Be specific in that start <= end and end <= len This is just one fix in response to #32057
2 parents bd01a47 + 55671a0 commit 27b1f09

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

src/libcore/str/mod.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,13 +1311,19 @@ mod traits {
13111311
}
13121312
}
13131313

1314+
/// Implements substring slicing with syntax `&self[begin .. end]`.
1315+
///
13141316
/// Returns a slice of the given string from the byte range
13151317
/// [`begin`..`end`).
13161318
///
13171319
/// This operation is `O(1)`.
13181320
///
1319-
/// Panics when `begin` and `end` do not point to valid characters
1320-
/// or point beyond the last character of the string.
1321+
/// # Panics
1322+
///
1323+
/// Panics if `begin` or `end` does not point to the starting
1324+
/// byte offset of a character (as defined by `is_char_boundary`).
1325+
/// Requires that `begin <= end` and `end <= len` where `len` is the
1326+
/// length of the string.
13211327
///
13221328
/// # Examples
13231329
///
@@ -1353,8 +1359,20 @@ mod traits {
13531359
}
13541360
}
13551361

1362+
/// Implements mutable substring slicing with syntax
1363+
/// `&mut self[begin .. end]`.
1364+
///
13561365
/// Returns a mutable slice of the given string from the byte range
13571366
/// [`begin`..`end`).
1367+
///
1368+
/// This operation is `O(1)`.
1369+
///
1370+
/// # Panics
1371+
///
1372+
/// Panics if `begin` or `end` does not point to the starting
1373+
/// byte offset of a character (as defined by `is_char_boundary`).
1374+
/// Requires that `begin <= end` and `end <= len` where `len` is the
1375+
/// length of the string.
13581376
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
13591377
impl ops::IndexMut<ops::Range<usize>> for str {
13601378
#[inline]
@@ -1370,13 +1388,12 @@ mod traits {
13701388
}
13711389
}
13721390

1373-
/// Returns a slice of the string from the beginning to byte
1374-
/// `end`.
1391+
/// Implements substring slicing with syntax `&self[.. end]`.
13751392
///
1376-
/// Equivalent to `self[0 .. end]`.
1393+
/// Returns a slice of the string from the beginning to byte offset
1394+
/// `end`.
13771395
///
1378-
/// Panics when `end` does not point to a valid character, or is
1379-
/// out of bounds.
1396+
/// Equivalent to `&self[0 .. end]`.
13801397
#[stable(feature = "rust1", since = "1.0.0")]
13811398
impl ops::Index<ops::RangeTo<usize>> for str {
13821399
type Output = str;
@@ -1392,8 +1409,12 @@ mod traits {
13921409
}
13931410
}
13941411

1395-
/// Returns a mutable slice of the string from the beginning to byte
1412+
/// Implements mutable substring slicing with syntax `&mut self[.. end]`.
1413+
///
1414+
/// Returns a mutable slice of the string from the beginning to byte offset
13961415
/// `end`.
1416+
///
1417+
/// Equivalent to `&mut self[0 .. end]`.
13971418
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
13981419
impl ops::IndexMut<ops::RangeTo<usize>> for str {
13991420
#[inline]
@@ -1407,12 +1428,12 @@ mod traits {
14071428
}
14081429
}
14091430

1410-
/// Returns a slice of the string from `begin` to its end.
1431+
/// Implements substring slicing with syntax `&self[begin ..]`.
14111432
///
1412-
/// Equivalent to `self[begin .. self.len()]`.
1433+
/// Returns a slice of the string from byte offset `begin`
1434+
/// to the end of the string.
14131435
///
1414-
/// Panics when `begin` does not point to a valid character, or is
1415-
/// out of bounds.
1436+
/// Equivalent to `&self[begin .. len]`.
14161437
#[stable(feature = "rust1", since = "1.0.0")]
14171438
impl ops::Index<ops::RangeFrom<usize>> for str {
14181439
type Output = str;
@@ -1428,7 +1449,12 @@ mod traits {
14281449
}
14291450
}
14301451

1431-
/// Returns a slice of the string from `begin` to its end.
1452+
/// Implements mutable substring slicing with syntax `&mut self[begin ..]`.
1453+
///
1454+
/// Returns a mutable slice of the string from byte offset `begin`
1455+
/// to the end of the string.
1456+
///
1457+
/// Equivalent to `&mut self[begin .. len]`.
14321458
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
14331459
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
14341460
#[inline]
@@ -1443,6 +1469,12 @@ mod traits {
14431469
}
14441470
}
14451471

1472+
/// Implements substring slicing with syntax `&self[..]`.
1473+
///
1474+
/// Returns a slice of the whole string. This operation can
1475+
/// never panic.
1476+
///
1477+
/// Equivalent to `&self[0 .. len]`.
14461478
#[stable(feature = "rust1", since = "1.0.0")]
14471479
impl ops::Index<ops::RangeFull> for str {
14481480
type Output = str;
@@ -1453,6 +1485,12 @@ mod traits {
14531485
}
14541486
}
14551487

1488+
/// Implements mutable substring slicing with syntax `&mut self[..]`.
1489+
///
1490+
/// Returns a mutable slice of the whole string. This operation can
1491+
/// never panic.
1492+
///
1493+
/// Equivalent to `&mut self[0 .. len]`.
14561494
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
14571495
impl ops::IndexMut<ops::RangeFull> for str {
14581496
#[inline]

0 commit comments

Comments
 (0)