Skip to main content

core/str/
traits.rs

1//! Trait implementations for `str`.
2
3#[cfg(not(feature = "ferrocene_subset"))]
4use super::ParseBoolError;
5#[cfg(not(feature = "ferrocene_subset"))]
6use crate::cmp::Ordering;
7use crate::intrinsics::unchecked_sub;
8use crate::slice::SliceIndex;
9use crate::ub_checks::assert_unsafe_precondition;
10#[cfg(not(feature = "ferrocene_subset"))]
11use crate::{ops, ptr, range};
12
13// Ferrocene addition: imports for certified subset
14#[cfg(feature = "ferrocene_subset")]
15#[rustfmt::skip]
16use crate::{ops, ptr};
17
18/// Implements ordering of strings.
19///
20/// Strings are ordered  [lexicographically](Ord#lexicographical-comparison) by their byte values. This orders Unicode code
21/// points based on their positions in the code charts. This is not necessarily the same as
22/// "alphabetical" order, which varies by language and locale. Sorting strings according to
23/// culturally-accepted standards requires locale-specific data that is outside the scope of
24/// the `str` type.
25#[stable(feature = "rust1", since = "1.0.0")]
26#[cfg(not(feature = "ferrocene_subset"))]
27impl Ord for str {
28    #[inline]
29    fn cmp(&self, other: &str) -> Ordering {
30        self.as_bytes().cmp(other.as_bytes())
31    }
32}
33
34#[stable(feature = "rust1", since = "1.0.0")]
35#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
36impl const PartialEq for str {
37    #[inline]
38    fn eq(&self, other: &str) -> bool {
39        self.as_bytes() == other.as_bytes()
40    }
41}
42
43#[stable(feature = "rust1", since = "1.0.0")]
44#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
45impl const Eq for str {}
46
47/// Implements comparison operations on strings.
48///
49/// Strings are compared [lexicographically](Ord#lexicographical-comparison) by their byte values. This compares Unicode code
50/// points based on their positions in the code charts. This is not necessarily the same as
51/// "alphabetical" order, which varies by language and locale. Comparing strings according to
52/// culturally-accepted standards requires locale-specific data that is outside the scope of
53/// the `str` type.
54#[stable(feature = "rust1", since = "1.0.0")]
55#[cfg(not(feature = "ferrocene_subset"))]
56impl PartialOrd for str {
57    #[inline]
58    fn partial_cmp(&self, other: &str) -> Option<Ordering> {
59        Some(self.cmp(other))
60    }
61}
62
63#[stable(feature = "rust1", since = "1.0.0")]
64#[rustc_const_unstable(feature = "const_index", issue = "143775")]
65impl<I> const ops::Index<I> for str
66where
67    I: [const] SliceIndex<str>,
68{
69    type Output = I::Output;
70
71    #[inline]
72    fn index(&self, index: I) -> &I::Output {
73        index.index(self)
74    }
75}
76
77#[stable(feature = "rust1", since = "1.0.0")]
78#[rustc_const_unstable(feature = "const_index", issue = "143775")]
79#[cfg(not(feature = "ferrocene_subset"))]
80impl<I> const ops::IndexMut<I> for str
81where
82    I: [const] SliceIndex<str>,
83{
84    #[inline]
85    fn index_mut(&mut self, index: I) -> &mut I::Output {
86        index.index_mut(self)
87    }
88}
89
90#[inline(never)]
91#[cold]
92#[track_caller]
93#[cfg(not(feature = "ferrocene_subset"))]
94const fn str_index_overflow_fail() -> ! {
95    panic!("attempted to index str up to maximum usize");
96}
97
98/// Implements substring slicing with syntax `&self[..]` or `&mut self[..]`.
99///
100/// Returns a slice of the whole string, i.e., returns `&self` or `&mut
101/// self`. Equivalent to `&self[0 .. len]` or `&mut self[0 .. len]`. Unlike
102/// other indexing operations, this can never panic.
103///
104/// This operation is *O*(1).
105///
106/// Prior to 1.20.0, these indexing operations were still supported by
107/// direct implementation of `Index` and `IndexMut`.
108///
109/// Equivalent to `&self[0 .. len]` or `&mut self[0 .. len]`.
110#[stable(feature = "str_checked_slicing", since = "1.20.0")]
111#[rustc_const_unstable(feature = "const_index", issue = "143775")]
112#[cfg(not(feature = "ferrocene_subset"))]
113unsafe impl const SliceIndex<str> for ops::RangeFull {
114    type Output = str;
115    #[inline]
116    fn get(self, slice: &str) -> Option<&Self::Output> {
117        Some(slice)
118    }
119    #[inline]
120    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
121        Some(slice)
122    }
123    #[inline]
124    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
125        slice
126    }
127    #[inline]
128    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
129        slice
130    }
131    #[inline]
132    fn index(self, slice: &str) -> &Self::Output {
133        slice
134    }
135    #[inline]
136    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
137        slice
138    }
139}
140
141/// Implements substring slicing with syntax `&self[begin .. end]` or `&mut
142/// self[begin .. end]`.
143///
144/// Returns a slice of the given string from the byte range
145/// [`begin`, `end`).
146///
147/// This operation is *O*(1).
148///
149/// Prior to 1.20.0, these indexing operations were still supported by
150/// direct implementation of `Index` and `IndexMut`.
151///
152/// # Panics
153///
154/// Panics if `begin` or `end` does not point to the starting byte offset of
155/// a character (as defined by `is_char_boundary`), if `begin > end`, or if
156/// `end > len`.
157///
158/// # Examples
159///
160/// ```
161/// let s = "Löwe 老虎 Léopard";
162/// assert_eq!(&s[0 .. 1], "L");
163///
164/// assert_eq!(&s[1 .. 9], "öwe 老");
165///
166/// // these will panic:
167/// // byte 2 lies within `ö`:
168/// // &s[2 ..3];
169///
170/// // byte 8 lies within `老`
171/// // &s[1 .. 8];
172///
173/// // byte 100 is outside the string
174/// // &s[3 .. 100];
175/// ```
176#[stable(feature = "str_checked_slicing", since = "1.20.0")]
177#[rustc_const_unstable(feature = "const_index", issue = "143775")]
178unsafe impl const SliceIndex<str> for ops::Range<usize> {
179    type Output = str;
180    #[inline]
181    fn get(self, slice: &str) -> Option<&Self::Output> {
182        if self.start <= self.end
183            && slice.is_char_boundary(self.start)
184            && slice.is_char_boundary(self.end)
185        {
186            // SAFETY: just checked that `start` and `end` are on a char boundary,
187            // and we are passing in a safe reference, so the return value will also be one.
188            // We also checked char boundaries, so this is valid UTF-8.
189            Some(unsafe { &*self.get_unchecked(slice) })
190        } else {
191            None
192        }
193    }
194    #[inline]
195    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
196        if self.start <= self.end
197            && slice.is_char_boundary(self.start)
198            && slice.is_char_boundary(self.end)
199        {
200            // SAFETY: just checked that `start` and `end` are on a char boundary.
201            // We know the pointer is unique because we got it from `slice`.
202            Some(unsafe { &mut *self.get_unchecked_mut(slice) })
203        } else {
204            None
205        }
206    }
207    #[inline]
208    #[track_caller]
209    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
210        let slice = slice as *const [u8];
211
212        assert_unsafe_precondition!(
213            // We'd like to check that the bounds are on char boundaries,
214            // but there's not really a way to do so without reading
215            // behind the pointer, which has aliasing implications.
216            // It's also not possible to move this check up to
217            // `str::get_unchecked` without adding a special function
218            // to `SliceIndex` just for this.
219            check_library_ub,
220            "str::get_unchecked requires that the range is within the string slice",
221            (
222                start: usize = self.start,
223                end: usize = self.end,
224                len: usize = slice.len()
225            ) => end >= start && end <= len,
226        );
227
228        // SAFETY: the caller guarantees that `self` is in bounds of `slice`
229        // which satisfies all the conditions for `add`.
230        unsafe {
231            let new_len = unchecked_sub(self.end, self.start);
232            ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len) as *const str
233        }
234    }
235    #[inline]
236    #[track_caller]
237    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
238        let slice = slice as *mut [u8];
239
240        assert_unsafe_precondition!(
241            check_library_ub,
242            "str::get_unchecked_mut requires that the range is within the string slice",
243            (
244                start: usize = self.start,
245                end: usize = self.end,
246                len: usize = slice.len()
247            ) => end >= start && end <= len,
248        );
249
250        // SAFETY: see comments for `get_unchecked`.
251        unsafe {
252            let new_len = unchecked_sub(self.end, self.start);
253            ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len) as *mut str
254        }
255    }
256    #[inline]
257    fn index(self, slice: &str) -> &Self::Output {
258        let (start, end) = (self.start, self.end);
259        match self.get(slice) {
260            Some(s) => s,
261            None => super::slice_error_fail(slice, start, end),
262        }
263    }
264    #[inline]
265    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
266        // is_char_boundary checks that the index is in [0, .len()]
267        // cannot reuse `get` as above, because of NLL trouble
268        if self.start <= self.end
269            && slice.is_char_boundary(self.start)
270            && slice.is_char_boundary(self.end)
271        {
272            // SAFETY: just checked that `start` and `end` are on a char boundary,
273            // and we are passing in a safe reference, so the return value will also be one.
274            unsafe { &mut *self.get_unchecked_mut(slice) }
275        } else {
276            super::slice_error_fail(slice, self.start, self.end)
277        }
278    }
279}
280
281#[unstable(feature = "new_range_api", issue = "125687")]
282#[rustc_const_unstable(feature = "const_index", issue = "143775")]
283#[cfg(not(feature = "ferrocene_subset"))]
284unsafe impl const SliceIndex<str> for range::Range<usize> {
285    type Output = str;
286    #[inline]
287    fn get(self, slice: &str) -> Option<&Self::Output> {
288        if self.start <= self.end
289            && slice.is_char_boundary(self.start)
290            && slice.is_char_boundary(self.end)
291        {
292            // SAFETY: just checked that `start` and `end` are on a char boundary,
293            // and we are passing in a safe reference, so the return value will also be one.
294            // We also checked char boundaries, so this is valid UTF-8.
295            Some(unsafe { &*self.get_unchecked(slice) })
296        } else {
297            None
298        }
299    }
300    #[inline]
301    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
302        if self.start <= self.end
303            && slice.is_char_boundary(self.start)
304            && slice.is_char_boundary(self.end)
305        {
306            // SAFETY: just checked that `start` and `end` are on a char boundary.
307            // We know the pointer is unique because we got it from `slice`.
308            Some(unsafe { &mut *self.get_unchecked_mut(slice) })
309        } else {
310            None
311        }
312    }
313    #[inline]
314    #[track_caller]
315    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
316        let slice = slice as *const [u8];
317
318        assert_unsafe_precondition!(
319            // We'd like to check that the bounds are on char boundaries,
320            // but there's not really a way to do so without reading
321            // behind the pointer, which has aliasing implications.
322            // It's also not possible to move this check up to
323            // `str::get_unchecked` without adding a special function
324            // to `SliceIndex` just for this.
325            check_library_ub,
326            "str::get_unchecked requires that the range is within the string slice",
327            (
328                start: usize = self.start,
329                end: usize = self.end,
330                len: usize = slice.len()
331            ) => end >= start && end <= len,
332        );
333
334        // SAFETY: the caller guarantees that `self` is in bounds of `slice`
335        // which satisfies all the conditions for `add`.
336        unsafe {
337            let new_len = unchecked_sub(self.end, self.start);
338            ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len) as *const str
339        }
340    }
341    #[inline]
342    #[track_caller]
343    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
344        let slice = slice as *mut [u8];
345
346        assert_unsafe_precondition!(
347            check_library_ub,
348            "str::get_unchecked_mut requires that the range is within the string slice",
349            (
350                start: usize = self.start,
351                end: usize = self.end,
352                len: usize = slice.len()
353            ) => end >= start && end <= len,
354        );
355
356        // SAFETY: see comments for `get_unchecked`.
357        unsafe {
358            let new_len = unchecked_sub(self.end, self.start);
359            ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len) as *mut str
360        }
361    }
362    #[inline]
363    fn index(self, slice: &str) -> &Self::Output {
364        let (start, end) = (self.start, self.end);
365        match self.get(slice) {
366            Some(s) => s,
367            None => super::slice_error_fail(slice, start, end),
368        }
369    }
370    #[inline]
371    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
372        // is_char_boundary checks that the index is in [0, .len()]
373        // cannot reuse `get` as above, because of NLL trouble
374        if self.start <= self.end
375            && slice.is_char_boundary(self.start)
376            && slice.is_char_boundary(self.end)
377        {
378            // SAFETY: just checked that `start` and `end` are on a char boundary,
379            // and we are passing in a safe reference, so the return value will also be one.
380            unsafe { &mut *self.get_unchecked_mut(slice) }
381        } else {
382            super::slice_error_fail(slice, self.start, self.end)
383        }
384    }
385}
386
387/// Implements substring slicing for arbitrary bounds.
388///
389/// Returns a slice of the given string bounded by the byte indices
390/// provided by each bound.
391///
392/// This operation is *O*(1).
393///
394/// # Panics
395///
396/// Panics if `begin` or `end` (if it exists and once adjusted for
397/// inclusion/exclusion) does not point to the starting byte offset of
398/// a character (as defined by `is_char_boundary`), if `begin > end`, or if
399/// `end > len`.
400#[stable(feature = "slice_index_str_with_ops_bound_pair", since = "1.73.0")]
401#[cfg(not(feature = "ferrocene_subset"))]
402unsafe impl SliceIndex<str> for (ops::Bound<usize>, ops::Bound<usize>) {
403    type Output = str;
404
405    #[inline]
406    fn get(self, slice: &str) -> Option<&str> {
407        crate::slice::index::into_range(slice.len(), self)?.get(slice)
408    }
409
410    #[inline]
411    fn get_mut(self, slice: &mut str) -> Option<&mut str> {
412        crate::slice::index::into_range(slice.len(), self)?.get_mut(slice)
413    }
414
415    #[inline]
416    unsafe fn get_unchecked(self, slice: *const str) -> *const str {
417        let len = (slice as *const [u8]).len();
418        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
419        unsafe { crate::slice::index::into_range_unchecked(len, self).get_unchecked(slice) }
420    }
421
422    #[inline]
423    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut str {
424        let len = (slice as *mut [u8]).len();
425        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
426        unsafe { crate::slice::index::into_range_unchecked(len, self).get_unchecked_mut(slice) }
427    }
428
429    #[inline]
430    fn index(self, slice: &str) -> &str {
431        crate::slice::index::into_slice_range(slice.len(), self).index(slice)
432    }
433
434    #[inline]
435    fn index_mut(self, slice: &mut str) -> &mut str {
436        crate::slice::index::into_slice_range(slice.len(), self).index_mut(slice)
437    }
438}
439
440/// Implements substring slicing with syntax `&self[.. end]` or `&mut
441/// self[.. end]`.
442///
443/// Returns a slice of the given string from the byte range \[0, `end`).
444/// Equivalent to `&self[0 .. end]` or `&mut self[0 .. end]`.
445///
446/// This operation is *O*(1).
447///
448/// Prior to 1.20.0, these indexing operations were still supported by
449/// direct implementation of `Index` and `IndexMut`.
450///
451/// # Panics
452///
453/// Panics if `end` does not point to the starting byte offset of a
454/// character (as defined by `is_char_boundary`), or if `end > len`.
455#[stable(feature = "str_checked_slicing", since = "1.20.0")]
456#[rustc_const_unstable(feature = "const_index", issue = "143775")]
457unsafe impl const SliceIndex<str> for ops::RangeTo<usize> {
458    type Output = str;
459    #[inline]
460    fn get(self, slice: &str) -> Option<&Self::Output> {
461        if slice.is_char_boundary(self.end) {
462            // SAFETY: just checked that `end` is on a char boundary,
463            // and we are passing in a safe reference, so the return value will also be one.
464            Some(unsafe { &*self.get_unchecked(slice) })
465        } else {
466            None
467        }
468    }
469    #[inline]
470    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
471        if slice.is_char_boundary(self.end) {
472            // SAFETY: just checked that `end` is on a char boundary,
473            // and we are passing in a safe reference, so the return value will also be one.
474            Some(unsafe { &mut *self.get_unchecked_mut(slice) })
475        } else {
476            None
477        }
478    }
479    #[inline]
480    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
481        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
482        unsafe { (0..self.end).get_unchecked(slice) }
483    }
484    #[inline]
485    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
486        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
487        unsafe { (0..self.end).get_unchecked_mut(slice) }
488    }
489    #[inline]
490    fn index(self, slice: &str) -> &Self::Output {
491        let end = self.end;
492        match self.get(slice) {
493            Some(s) => s,
494            None => super::slice_error_fail(slice, 0, end),
495        }
496    }
497    #[inline]
498    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
499        if slice.is_char_boundary(self.end) {
500            // SAFETY: just checked that `end` is on a char boundary,
501            // and we are passing in a safe reference, so the return value will also be one.
502            unsafe { &mut *self.get_unchecked_mut(slice) }
503        } else {
504            super::slice_error_fail(slice, 0, self.end)
505        }
506    }
507}
508
509/// Implements substring slicing with syntax `&self[begin ..]` or `&mut
510/// self[begin ..]`.
511///
512/// Returns a slice of the given string from the byte range \[`begin`, `len`).
513/// Equivalent to `&self[begin .. len]` or `&mut self[begin .. len]`.
514///
515/// This operation is *O*(1).
516///
517/// Prior to 1.20.0, these indexing operations were still supported by
518/// direct implementation of `Index` and `IndexMut`.
519///
520/// # Panics
521///
522/// Panics if `begin` does not point to the starting byte offset of
523/// a character (as defined by `is_char_boundary`), or if `begin > len`.
524#[stable(feature = "str_checked_slicing", since = "1.20.0")]
525#[rustc_const_unstable(feature = "const_index", issue = "143775")]
526unsafe impl const SliceIndex<str> for ops::RangeFrom<usize> {
527    type Output = str;
528    #[inline]
529    fn get(self, slice: &str) -> Option<&Self::Output> {
530        if slice.is_char_boundary(self.start) {
531            // SAFETY: just checked that `start` is on a char boundary,
532            // and we are passing in a safe reference, so the return value will also be one.
533            Some(unsafe { &*self.get_unchecked(slice) })
534        } else {
535            None
536        }
537    }
538    #[inline]
539    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
540        if slice.is_char_boundary(self.start) {
541            // SAFETY: just checked that `start` is on a char boundary,
542            // and we are passing in a safe reference, so the return value will also be one.
543            Some(unsafe { &mut *self.get_unchecked_mut(slice) })
544        } else {
545            None
546        }
547    }
548    #[inline]
549    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
550        let len = (slice as *const [u8]).len();
551        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
552        unsafe { (self.start..len).get_unchecked(slice) }
553    }
554    #[inline]
555    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
556        let len = (slice as *mut [u8]).len();
557        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
558        unsafe { (self.start..len).get_unchecked_mut(slice) }
559    }
560    #[inline]
561    fn index(self, slice: &str) -> &Self::Output {
562        let (start, end) = (self.start, slice.len());
563        match self.get(slice) {
564            Some(s) => s,
565            None => super::slice_error_fail(slice, start, end),
566        }
567    }
568    #[inline]
569    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
570        if slice.is_char_boundary(self.start) {
571            // SAFETY: just checked that `start` is on a char boundary,
572            // and we are passing in a safe reference, so the return value will also be one.
573            unsafe { &mut *self.get_unchecked_mut(slice) }
574        } else {
575            super::slice_error_fail(slice, self.start, slice.len())
576        }
577    }
578}
579
580#[unstable(feature = "new_range_api", issue = "125687")]
581#[rustc_const_unstable(feature = "const_index", issue = "143775")]
582#[cfg(not(feature = "ferrocene_subset"))]
583unsafe impl const SliceIndex<str> for range::RangeFrom<usize> {
584    type Output = str;
585    #[inline]
586    fn get(self, slice: &str) -> Option<&Self::Output> {
587        if slice.is_char_boundary(self.start) {
588            // SAFETY: just checked that `start` is on a char boundary,
589            // and we are passing in a safe reference, so the return value will also be one.
590            Some(unsafe { &*self.get_unchecked(slice) })
591        } else {
592            None
593        }
594    }
595    #[inline]
596    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
597        if slice.is_char_boundary(self.start) {
598            // SAFETY: just checked that `start` is on a char boundary,
599            // and we are passing in a safe reference, so the return value will also be one.
600            Some(unsafe { &mut *self.get_unchecked_mut(slice) })
601        } else {
602            None
603        }
604    }
605    #[inline]
606    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
607        let len = (slice as *const [u8]).len();
608        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
609        unsafe { (self.start..len).get_unchecked(slice) }
610    }
611    #[inline]
612    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
613        let len = (slice as *mut [u8]).len();
614        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
615        unsafe { (self.start..len).get_unchecked_mut(slice) }
616    }
617    #[inline]
618    fn index(self, slice: &str) -> &Self::Output {
619        let (start, end) = (self.start, slice.len());
620        match self.get(slice) {
621            Some(s) => s,
622            None => super::slice_error_fail(slice, start, end),
623        }
624    }
625    #[inline]
626    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
627        if slice.is_char_boundary(self.start) {
628            // SAFETY: just checked that `start` is on a char boundary,
629            // and we are passing in a safe reference, so the return value will also be one.
630            unsafe { &mut *self.get_unchecked_mut(slice) }
631        } else {
632            super::slice_error_fail(slice, self.start, slice.len())
633        }
634    }
635}
636
637/// Implements substring slicing with syntax `&self[begin ..= end]` or `&mut
638/// self[begin ..= end]`.
639///
640/// Returns a slice of the given string from the byte range
641/// [`begin`, `end`]. Equivalent to `&self [begin .. end + 1]` or `&mut
642/// self[begin .. end + 1]`, except if `end` has the maximum value for
643/// `usize`.
644///
645/// This operation is *O*(1).
646///
647/// # Panics
648///
649/// Panics if `begin` does not point to the starting byte offset of
650/// a character (as defined by `is_char_boundary`), if `end` does not point
651/// to the ending byte offset of a character (`end + 1` is either a starting
652/// byte offset or equal to `len`), if `begin > end`, or if `end >= len`.
653#[stable(feature = "inclusive_range", since = "1.26.0")]
654#[rustc_const_unstable(feature = "const_index", issue = "143775")]
655#[cfg(not(feature = "ferrocene_subset"))]
656unsafe impl const SliceIndex<str> for ops::RangeInclusive<usize> {
657    type Output = str;
658    #[inline]
659    fn get(self, slice: &str) -> Option<&Self::Output> {
660        if *self.end() == usize::MAX { None } else { self.into_slice_range().get(slice) }
661    }
662    #[inline]
663    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
664        if *self.end() == usize::MAX { None } else { self.into_slice_range().get_mut(slice) }
665    }
666    #[inline]
667    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
668        // SAFETY: the caller must uphold the safety contract for `get_unchecked`.
669        unsafe { self.into_slice_range().get_unchecked(slice) }
670    }
671    #[inline]
672    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
673        // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`.
674        unsafe { self.into_slice_range().get_unchecked_mut(slice) }
675    }
676    #[inline]
677    fn index(self, slice: &str) -> &Self::Output {
678        if *self.end() == usize::MAX {
679            str_index_overflow_fail();
680        }
681        self.into_slice_range().index(slice)
682    }
683    #[inline]
684    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
685        if *self.end() == usize::MAX {
686            str_index_overflow_fail();
687        }
688        self.into_slice_range().index_mut(slice)
689    }
690}
691
692#[unstable(feature = "new_range_api", issue = "125687")]
693#[rustc_const_unstable(feature = "const_index", issue = "143775")]
694#[cfg(not(feature = "ferrocene_subset"))]
695unsafe impl const SliceIndex<str> for range::RangeInclusive<usize> {
696    type Output = str;
697    #[inline]
698    fn get(self, slice: &str) -> Option<&Self::Output> {
699        if self.last == usize::MAX { None } else { self.into_slice_range().get(slice) }
700    }
701    #[inline]
702    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
703        if self.last == usize::MAX { None } else { self.into_slice_range().get_mut(slice) }
704    }
705    #[inline]
706    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
707        // SAFETY: the caller must uphold the safety contract for `get_unchecked`.
708        unsafe { self.into_slice_range().get_unchecked(slice) }
709    }
710    #[inline]
711    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
712        // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`.
713        unsafe { self.into_slice_range().get_unchecked_mut(slice) }
714    }
715    #[inline]
716    fn index(self, slice: &str) -> &Self::Output {
717        if self.last == usize::MAX {
718            str_index_overflow_fail();
719        }
720        self.into_slice_range().index(slice)
721    }
722    #[inline]
723    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
724        if self.last == usize::MAX {
725            str_index_overflow_fail();
726        }
727        self.into_slice_range().index_mut(slice)
728    }
729}
730
731/// Implements substring slicing with syntax `&self[..= end]` or `&mut
732/// self[..= end]`.
733///
734/// Returns a slice of the given string from the byte range \[0, `end`\].
735/// Equivalent to `&self [0 .. end + 1]`, except if `end` has the maximum
736/// value for `usize`.
737///
738/// This operation is *O*(1).
739///
740/// # Panics
741///
742/// Panics if `end` does not point to the ending byte offset of a character
743/// (`end + 1` is either a starting byte offset as defined by
744/// `is_char_boundary`, or equal to `len`), or if `end >= len`.
745#[stable(feature = "inclusive_range", since = "1.26.0")]
746#[rustc_const_unstable(feature = "const_index", issue = "143775")]
747#[cfg(not(feature = "ferrocene_subset"))]
748unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
749    type Output = str;
750    #[inline]
751    fn get(self, slice: &str) -> Option<&Self::Output> {
752        (0..=self.end).get(slice)
753    }
754    #[inline]
755    fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
756        (0..=self.end).get_mut(slice)
757    }
758    #[inline]
759    unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
760        // SAFETY: the caller must uphold the safety contract for `get_unchecked`.
761        unsafe { (0..=self.end).get_unchecked(slice) }
762    }
763    #[inline]
764    unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
765        // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`.
766        unsafe { (0..=self.end).get_unchecked_mut(slice) }
767    }
768    #[inline]
769    fn index(self, slice: &str) -> &Self::Output {
770        (0..=self.end).index(slice)
771    }
772    #[inline]
773    fn index_mut(self, slice: &mut str) -> &mut Self::Output {
774        (0..=self.end).index_mut(slice)
775    }
776}
777
778/// Parse a value from a string
779///
780/// `FromStr`'s [`from_str`] method is often used implicitly, through
781/// [`str`]'s [`parse`] method. See [`parse`]'s documentation for examples.
782///
783/// [`from_str`]: FromStr::from_str
784/// [`parse`]: str::parse
785///
786/// `FromStr` does not have a lifetime parameter, and so you can only parse types
787/// that do not contain a lifetime parameter themselves. In other words, you can
788/// parse an `i32` with `FromStr`, but not a `&i32`. You can parse a struct that
789/// contains an `i32`, but not one that contains an `&i32`.
790///
791/// # Input format and round-tripping
792///
793/// The input format expected by a type's `FromStr` implementation depends on the type. Check the
794/// type's documentation for the input formats it knows how to parse. Note that the input format of
795/// a type's `FromStr` implementation might not necessarily accept the output format of its
796/// `Display` implementation, and even if it does, the `Display` implementation may not be lossless
797/// so the round-trip may lose information.
798///
799/// However, if a type has a lossless `Display` implementation whose output is meant to be
800/// conveniently machine-parseable and not just meant for human consumption, then the type may wish
801/// to accept the same format in `FromStr`, and document that usage. Having both `Display` and
802/// `FromStr` implementations where the result of `Display` cannot be parsed with `FromStr` may
803/// surprise users.
804///
805/// # Examples
806///
807/// Basic implementation of `FromStr` on an example `Point` type:
808///
809/// ```
810/// use std::str::FromStr;
811///
812/// #[derive(Debug, PartialEq)]
813/// struct Point {
814///     x: i32,
815///     y: i32
816/// }
817///
818/// #[derive(Debug, PartialEq, Eq)]
819/// struct ParsePointError;
820///
821/// impl FromStr for Point {
822///     type Err = ParsePointError;
823///
824///     fn from_str(s: &str) -> Result<Self, Self::Err> {
825///         let (x, y) = s
826///             .strip_prefix('(')
827///             .and_then(|s| s.strip_suffix(')'))
828///             .and_then(|s| s.split_once(','))
829///             .ok_or(ParsePointError)?;
830///
831///         let x_fromstr = x.parse::<i32>().map_err(|_| ParsePointError)?;
832///         let y_fromstr = y.parse::<i32>().map_err(|_| ParsePointError)?;
833///
834///         Ok(Point { x: x_fromstr, y: y_fromstr })
835///     }
836/// }
837///
838/// let expected = Ok(Point { x: 1, y: 2 });
839/// // Explicit call
840/// assert_eq!(Point::from_str("(1,2)"), expected);
841/// // Implicit calls, through parse
842/// assert_eq!("(1,2)".parse(), expected);
843/// assert_eq!("(1,2)".parse::<Point>(), expected);
844/// // Invalid input string
845/// assert!(Point::from_str("(1 2)").is_err());
846/// ```
847#[stable(feature = "rust1", since = "1.0.0")]
848#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
849pub const trait FromStr: Sized {
850    /// The associated error which can be returned from parsing.
851    #[stable(feature = "rust1", since = "1.0.0")]
852    type Err;
853
854    /// Parses a string `s` to return a value of this type.
855    ///
856    /// If parsing succeeds, return the value inside [`Ok`], otherwise
857    /// when the string is ill-formatted return an error specific to the
858    /// inside [`Err`]. The error type is specific to the implementation of the trait.
859    ///
860    /// # Examples
861    ///
862    /// Basic usage with [`i32`], a type that implements `FromStr`:
863    ///
864    /// ```
865    /// use std::str::FromStr;
866    ///
867    /// let s = "5";
868    /// let x = i32::from_str(s).unwrap();
869    ///
870    /// assert_eq!(5, x);
871    /// ```
872    #[stable(feature = "rust1", since = "1.0.0")]
873    #[rustc_diagnostic_item = "from_str_method"]
874    fn from_str(s: &str) -> Result<Self, Self::Err>;
875}
876
877#[stable(feature = "rust1", since = "1.0.0")]
878#[cfg(not(feature = "ferrocene_subset"))]
879impl FromStr for bool {
880    type Err = ParseBoolError;
881
882    /// Parse a `bool` from a string.
883    ///
884    /// The only accepted values are `"true"` and `"false"`. Any other input
885    /// will return an error.
886    ///
887    /// # Examples
888    ///
889    /// ```
890    /// use std::str::FromStr;
891    ///
892    /// assert_eq!(FromStr::from_str("true"), Ok(true));
893    /// assert_eq!(FromStr::from_str("false"), Ok(false));
894    /// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
895    /// ```
896    ///
897    /// Note, in many cases, the `.parse()` method on `str` is more proper.
898    ///
899    /// ```
900    /// assert_eq!("true".parse(), Ok(true));
901    /// assert_eq!("false".parse(), Ok(false));
902    /// assert!("not even a boolean".parse::<bool>().is_err());
903    /// ```
904    #[inline]
905    fn from_str(s: &str) -> Result<bool, ParseBoolError> {
906        match s {
907            "true" => Ok(true),
908            "false" => Ok(false),
909            _ => Err(ParseBoolError),
910        }
911    }
912}