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