Skip to content

Commit 55671a0

Browse files
committed
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
1 parent c97524b commit 55671a0

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

src/libcore/str/mod.rs

+51-13
Original file line numberDiff line numberDiff line change
@@ -1313,13 +1313,19 @@ mod traits {
13131313
}
13141314
}
13151315

1316+
/// Implements substring slicing with syntax `&self[begin .. end]`.
1317+
///
13161318
/// Returns a slice of the given string from the byte range
13171319
/// [`begin`..`end`).
13181320
///
13191321
/// This operation is `O(1)`.
13201322
///
1321-
/// Panics when `begin` and `end` do not point to valid characters
1322-
/// or point beyond the last character of the string.
1323+
/// # Panics
1324+
///
1325+
/// Panics if `begin` or `end` does not point to the starting
1326+
/// byte offset of a character (as defined by `is_char_boundary`).
1327+
/// Requires that `begin <= end` and `end <= len` where `len` is the
1328+
/// length of the string.
13231329
///
13241330
/// # Examples
13251331
///
@@ -1355,8 +1361,20 @@ mod traits {
13551361
}
13561362
}
13571363

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

1375-
/// Returns a slice of the string from the beginning to byte
1376-
/// `end`.
1393+
/// Implements substring slicing with syntax `&self[.. end]`.
13771394
///
1378-
/// Equivalent to `self[0 .. end]`.
1395+
/// Returns a slice of the string from the beginning to byte offset
1396+
/// `end`.
13791397
///
1380-
/// Panics when `end` does not point to a valid character, or is
1381-
/// out of bounds.
1398+
/// Equivalent to `&self[0 .. end]`.
13821399
#[stable(feature = "rust1", since = "1.0.0")]
13831400
impl ops::Index<ops::RangeTo<usize>> for str {
13841401
type Output = str;
@@ -1394,8 +1411,12 @@ mod traits {
13941411
}
13951412
}
13961413

1397-
/// Returns a mutable slice of the string from the beginning to byte
1414+
/// Implements mutable substring slicing with syntax `&mut self[.. end]`.
1415+
///
1416+
/// Returns a mutable slice of the string from the beginning to byte offset
13981417
/// `end`.
1418+
///
1419+
/// Equivalent to `&mut self[0 .. end]`.
13991420
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
14001421
impl ops::IndexMut<ops::RangeTo<usize>> for str {
14011422
#[inline]
@@ -1409,12 +1430,12 @@ mod traits {
14091430
}
14101431
}
14111432

1412-
/// Returns a slice of the string from `begin` to its end.
1433+
/// Implements substring slicing with syntax `&self[begin ..]`.
14131434
///
1414-
/// Equivalent to `self[begin .. self.len()]`.
1435+
/// Returns a slice of the string from byte offset `begin`
1436+
/// to the end of the string.
14151437
///
1416-
/// Panics when `begin` does not point to a valid character, or is
1417-
/// out of bounds.
1438+
/// Equivalent to `&self[begin .. len]`.
14181439
#[stable(feature = "rust1", since = "1.0.0")]
14191440
impl ops::Index<ops::RangeFrom<usize>> for str {
14201441
type Output = str;
@@ -1430,7 +1451,12 @@ mod traits {
14301451
}
14311452
}
14321453

1433-
/// Returns a slice of the string from `begin` to its end.
1454+
/// Implements mutable substring slicing with syntax `&mut self[begin ..]`.
1455+
///
1456+
/// Returns a mutable slice of the string from byte offset `begin`
1457+
/// to the end of the string.
1458+
///
1459+
/// Equivalent to `&mut self[begin .. len]`.
14341460
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
14351461
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
14361462
#[inline]
@@ -1445,6 +1471,12 @@ mod traits {
14451471
}
14461472
}
14471473

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

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

0 commit comments

Comments
 (0)