@@ -352,6 +352,15 @@ impl<'a> Iterator<char> for Decompositions<'a> {
352
352
/// # Return value
353
353
///
354
354
/// The original string with all occurrences of `from` replaced with `to`
355
+ ///
356
+ /// # Example
357
+ ///
358
+ /// ```rust
359
+ /// use std::str;
360
+ /// let string = "orange";
361
+ /// let new_string = str::replace(string, "or", "str");
362
+ /// assert_eq!(new_string.as_slice(), "strange");
363
+ /// ```
355
364
pub fn replace ( s : & str , from : & str , to : & str ) -> String {
356
365
let mut result = String :: new ( ) ;
357
366
let mut last_end = 0 ;
@@ -573,6 +582,14 @@ pub type SendStr = MaybeOwned<'static>;
573
582
574
583
impl < ' a > MaybeOwned < ' a > {
575
584
/// Returns `true` if this `MaybeOwned` wraps an owned string
585
+ ///
586
+ /// # Example
587
+ ///
588
+ /// ```rust
589
+ /// let string = String::from_str("orange");
590
+ /// let maybe_owned_string = string.into_maybe_owned();
591
+ /// assert_eq!(true, maybe_owned_string.is_owned());
592
+ /// ```
576
593
#[ inline]
577
594
pub fn is_owned ( & self ) -> bool {
578
595
match * self {
@@ -582,6 +599,14 @@ impl<'a> MaybeOwned<'a> {
582
599
}
583
600
584
601
/// Returns `true` if this `MaybeOwned` wraps a borrowed string
602
+ ///
603
+ /// # Example
604
+ ///
605
+ /// ```rust
606
+ /// let string = "orange";
607
+ /// let maybe_owned_string = string.as_slice().into_maybe_owned();
608
+ /// assert_eq!(true, maybe_owned_string.is_slice());
609
+ /// ```
585
610
#[ inline]
586
611
pub fn is_slice ( & self ) -> bool {
587
612
match * self {
@@ -597,18 +622,40 @@ pub trait IntoMaybeOwned<'a> {
597
622
fn into_maybe_owned ( self ) -> MaybeOwned < ' a > ;
598
623
}
599
624
625
+ /// # Example
626
+ ///
627
+ /// ```rust
628
+ /// let owned_string = String::from_str("orange");
629
+ /// let maybe_owned_string = owned_string.into_maybe_owned();
630
+ /// assert_eq!(true, maybe_owned_string.is_owned());
631
+ /// ```
600
632
impl < ' a > IntoMaybeOwned < ' a > for String {
601
633
#[ inline]
602
634
fn into_maybe_owned ( self ) -> MaybeOwned < ' a > {
603
635
Owned ( self )
604
636
}
605
637
}
606
638
639
+ /// # Example
640
+ ///
641
+ /// ```rust
642
+ /// let string = "orange";
643
+ /// let maybe_owned_str = string.as_slice().into_maybe_owned();
644
+ /// assert_eq!(false, maybe_owned_str.is_owned());
645
+ /// ```
607
646
impl < ' a > IntoMaybeOwned < ' a > for & ' a str {
608
647
#[ inline]
609
648
fn into_maybe_owned ( self ) -> MaybeOwned < ' a > { Slice ( self ) }
610
649
}
611
650
651
+ /// # Example
652
+ ///
653
+ /// ```rust
654
+ /// let str = "orange";
655
+ /// let maybe_owned_str = str.as_slice().into_maybe_owned();
656
+ /// let maybe_maybe_owned_str = maybe_owned_str.into_maybe_owned();
657
+ /// assert_eq!(false, maybe_maybe_owned_str.is_owned());
658
+ /// ```
612
659
impl < ' a > IntoMaybeOwned < ' a > for MaybeOwned < ' a > {
613
660
#[ inline]
614
661
fn into_maybe_owned ( self ) -> MaybeOwned < ' a > { self }
0 commit comments