@@ -640,7 +640,8 @@ impl str {
640
640
#[ inline]
641
641
#[ must_use]
642
642
#[ stable( feature = "str_split_at" , since = "1.4.0" ) ]
643
- pub fn split_at ( & self , mid : usize ) -> ( & str , & str ) {
643
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "131518" ) ]
644
+ pub const fn split_at ( & self , mid : usize ) -> ( & str , & str ) {
644
645
match self . split_at_checked ( mid) {
645
646
None => slice_error_fail ( self , 0 , mid) ,
646
647
Some ( pair) => pair,
@@ -680,7 +681,8 @@ impl str {
680
681
#[ inline]
681
682
#[ must_use]
682
683
#[ stable( feature = "str_split_at" , since = "1.4.0" ) ]
683
- pub fn split_at_mut ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
684
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "131518" ) ]
685
+ pub const fn split_at_mut ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
684
686
// is_char_boundary checks that the index is in [0, .len()]
685
687
if self . is_char_boundary ( mid) {
686
688
// SAFETY: just checked that `mid` is on a char boundary.
@@ -719,11 +721,12 @@ impl str {
719
721
#[ inline]
720
722
#[ must_use]
721
723
#[ stable( feature = "split_at_checked" , since = "1.80.0" ) ]
722
- pub fn split_at_checked ( & self , mid : usize ) -> Option < ( & str , & str ) > {
724
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "131518" ) ]
725
+ pub const fn split_at_checked ( & self , mid : usize ) -> Option < ( & str , & str ) > {
723
726
// is_char_boundary checks that the index is in [0, .len()]
724
727
if self . is_char_boundary ( mid) {
725
728
// SAFETY: just checked that `mid` is on a char boundary.
726
- Some ( unsafe { ( self . get_unchecked ( 0 .. mid) , self . get_unchecked ( mid.. self . len ( ) ) ) } )
729
+ Some ( unsafe { self . split_at_unchecked ( mid) } )
727
730
} else {
728
731
None
729
732
}
@@ -759,7 +762,9 @@ impl str {
759
762
#[ inline]
760
763
#[ must_use]
761
764
#[ stable( feature = "split_at_checked" , since = "1.80.0" ) ]
762
- pub fn split_at_mut_checked ( & mut self , mid : usize ) -> Option < ( & mut str , & mut str ) > {
765
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "131518" ) ]
766
+ #[ rustc_allow_const_fn_unstable( const_is_char_boundary) ]
767
+ pub const fn split_at_mut_checked ( & mut self , mid : usize ) -> Option < ( & mut str , & mut str ) > {
763
768
// is_char_boundary checks that the index is in [0, .len()]
764
769
if self . is_char_boundary ( mid) {
765
770
// SAFETY: just checked that `mid` is on a char boundary.
@@ -775,7 +780,25 @@ impl str {
775
780
///
776
781
/// The caller must ensure that `mid` is a valid byte offset from the start
777
782
/// of the string and falls on the boundary of a UTF-8 code point.
778
- unsafe fn split_at_mut_unchecked ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
783
+ const unsafe fn split_at_unchecked ( & self , mid : usize ) -> ( & str , & str ) {
784
+ let len = self . len ( ) ;
785
+ let ptr = self . as_ptr ( ) ;
786
+ // SAFETY: caller guarantees `mid` is on a char boundary.
787
+ unsafe {
788
+ (
789
+ from_utf8_unchecked ( slice:: from_raw_parts ( ptr, mid) ) ,
790
+ from_utf8_unchecked ( slice:: from_raw_parts ( ptr. add ( mid) , len - mid) ) ,
791
+ )
792
+ }
793
+ }
794
+
795
+ /// Divides one string slice into two at an index.
796
+ ///
797
+ /// # Safety
798
+ ///
799
+ /// The caller must ensure that `mid` is a valid byte offset from the start
800
+ /// of the string and falls on the boundary of a UTF-8 code point.
801
+ const unsafe fn split_at_mut_unchecked ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
779
802
let len = self . len ( ) ;
780
803
let ptr = self . as_mut_ptr ( ) ;
781
804
// SAFETY: caller guarantees `mid` is on a char boundary.
0 commit comments