Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,18 @@ impl str {
/// The caller must ensure that `mid` is a valid byte offset from the start
/// of the string and falls on the boundary of a UTF-8 code point.
#[inline]
const unsafe fn split_at_unchecked(&self, mid: usize) -> (&str, &str) {
#[must_use]
#[unstable(feature = "str_split_at_unchecked", issue = "165545")]
pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&str, &str) {
let len = self.len();
let ptr = self.as_ptr();

assert_unsafe_precondition!(
check_library_ub,
"str::split_at_unchecked requires the index to be on a UTF-8 boundary",
(it: &str = self, mid: usize = mid) => it.is_char_boundary(mid),
);

// SAFETY: caller guarantees `mid` is on a char boundary.
Comment on lines 1004 to 1012

@yhx-12243 yhx-12243 Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this rust-lang/libs-team#938 (comment), I think it's good to add a ub-check, should we commit directly on this PR or create another?

Suggested change
let ptr = self.as_ptr();
// SAFETY: caller guarantees `mid` is on a char boundary.
let ptr = self.as_ptr();
assert_unsafe_precondition!(
check_library_ub,
"str::split_at_unchecked requires the index to be on the UTF-8 boundary",
(it: &str = self, mid: usize = mid) => it.is_char_boundary(mid),
);
// SAFETY: caller guarantees `mid` is on a char boundary.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it completely slipped my mind that this wouldn't have them because it was private. Since a rollup hasn't started I'll unapprove this and you can add them into this PR.

unsafe {
(
Expand All @@ -1015,9 +1024,19 @@ impl str {
///
/// The caller must ensure that `mid` is a valid byte offset from the start
/// of the string and falls on the boundary of a UTF-8 code point.
const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut str, &mut str) {
#[inline]
#[must_use]
#[unstable(feature = "str_split_at_unchecked", issue = "165545")]
pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut str, &mut str) {
let len = self.len();
let ptr = self.as_mut_ptr();

assert_unsafe_precondition!(
check_library_ub,
"str::split_at_mut_unchecked requires the index to be on a UTF-8 boundary",
(it: &str = self, mid: usize = mid) => it.is_char_boundary(mid),
);

// SAFETY: caller guarantees `mid` is on a char boundary.
unsafe {
(
Expand Down
8 changes: 7 additions & 1 deletion library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,13 @@ impl OsStr {
///
/// Any caller must ensure `mid` is within bounds and lies on
/// a valid `OsStr` boundary for the platform.
unsafe fn split_at_unchecked(&self, mid: usize) -> (&OsStr, &OsStr) {
#[unstable(feature = "str_split_at_unchecked", issue = "165545")]
pub unsafe fn split_at_unchecked(&self, mid: usize) -> (&OsStr, &OsStr) {
debug_assert!(
self.inner.try_check_public_boundary(mid).is_some(),
"OsStr::split_at_unchecked requires the index to be on a valid `OsStr` boundary",
);

// SAFETY: it's up to the caller to ensure this is safe.

@hkBst hkBst Nov 6, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also be #[inline] and #[must_use]?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and we may report to the original #156199, who had not added those.

unsafe {
let (first, second) = self.as_encoded_bytes().split_at_unchecked(mid);
Expand Down
Loading