Skip to content

Commit 803eca4

Browse files
committed
Add Invariant section to String's docs, analogous to str.
Documents that invalid UTF-8 in a `String` is not immediate UB, but other code may assume that `String`s are valid UTF-8, so it can lead to UB later.
1 parent ce3a5ce commit 803eca4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

library/alloc/src/string.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ use crate::vec::Vec;
350350
///
351351
/// Here, there's no need to allocate more memory inside the loop.
352352
///
353+
/// # Invariant
354+
///
355+
/// Rust libraries may assume that `String`s are always valid UTF-8, [just like `str`s](str#invariant).
356+
///
357+
/// Constructing a non-UTF-8 `String` is not immediate undefined behavior, but any function
358+
/// called on a `String` may assume that it is valid UTF-8, which means that a non-UTF-8 `String`
359+
/// can lead to undefined behavior down the road.
360+
///
353361
/// [str]: prim@str "str"
354362
/// [`str`]: prim@str "str"
355363
/// [`&str`]: prim@str "&str"
@@ -1023,7 +1031,7 @@ impl String {
10231031
/// This function is unsafe because it does not check that the bytes passed
10241032
/// to it are valid UTF-8. If this constraint is violated, it may cause
10251033
/// memory unsafety issues with future users of the `String`, as the rest of
1026-
/// the standard library assumes that `String`s are valid UTF-8.
1034+
/// the standard library [assumes that `String`s are valid UTF-8](String#invariant).
10271035
///
10281036
/// # Examples
10291037
///
@@ -1775,8 +1783,8 @@ impl String {
17751783
/// This function is unsafe because the returned `&mut Vec` allows writing
17761784
/// bytes which are not valid UTF-8. If this constraint is violated, using
17771785
/// the original `String` after dropping the `&mut Vec` may violate memory
1778-
/// safety, as the rest of the standard library assumes that `String`s are
1779-
/// valid UTF-8.
1786+
/// safety, as the rest of the standard library [assumes that `String`s are
1787+
/// valid UTF-8](String#invariant).
17801788
///
17811789
/// # Examples
17821790
///

0 commit comments

Comments
 (0)