diff --git a/CHANGELOG.md b/CHANGELOG.md index f710f83b05..af45697e79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). The implementation for sizes higher than u16::MAX were unsound anyway. - Added `shift_remove`, `shift_remove_entry` and `shift_remove_full` to `IndexMap` and `IndexSet`. - Implemented `Index` and `IndexMut` for `Vec`. +- Made `String::from_utf8_unchecked` const. The v0.9.0 changelog already listed this, but the change was undone before that release. ## [v0.9.3] 2025-04-15 diff --git a/src/string/mod.rs b/src/string/mod.rs index e61ab866d2..79f96d2a49 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -277,7 +277,7 @@ impl String { /// assert_eq!("💖", sparkle_heart); /// ``` #[inline] - pub unsafe fn from_utf8_unchecked(vec: Vec) -> Self { + pub const unsafe fn from_utf8_unchecked(vec: Vec) -> Self { Self { vec } } @@ -1116,6 +1116,13 @@ mod tests { static mut _S: String<8> = String::new(); } + #[test] + fn static_from_utf8_unchecked() { + // SAFETY: `b"abcd"` is valid UTF-8. + static S: String<8> = unsafe { String::from_utf8_unchecked(Vec::from_array(*b"abcd")) }; + assert_eq!(S, "abcd"); + } + #[test] fn clone() { let s1: String<20> = String::try_from("abcd").unwrap();