Skip to content

Commit 7158e8a

Browse files
committed
Add example for str replace() and MaybeOwned
- for 3 implementations of into_maybe_owned() - is_slice() - is_owned()
1 parent f78d2f5 commit 7158e8a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/libcollections/str.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ impl<'a> Iterator<char> for Decompositions<'a> {
351351
/// # Return value
352352
///
353353
/// The original string with all occurrences of `from` replaced with `to`
354+
///
355+
/// # Example
356+
///
357+
/// ```rust
358+
/// use std::str;
359+
/// let string = "orange";
360+
/// let new_string = str::replace(string, "or", "str");
361+
/// assert_eq!(new_string.as_slice(), "strange");
362+
/// ```
354363
pub fn replace(s: &str, from: &str, to: &str) -> String {
355364
let mut result = String::new();
356365
let mut last_end = 0;
@@ -572,6 +581,14 @@ pub type SendStr = MaybeOwned<'static>;
572581

573582
impl<'a> MaybeOwned<'a> {
574583
/// Returns `true` if this `MaybeOwned` wraps an owned string
584+
///
585+
/// # Example
586+
///
587+
/// ```rust
588+
/// let string = String::from_str("orange");
589+
/// let maybe_owned_string = string.into_maybe_owned();
590+
/// assert_eq!(true, maybe_owned_string.is_owned());
591+
/// ```
575592
#[inline]
576593
pub fn is_owned(&self) -> bool {
577594
match *self {
@@ -581,6 +598,14 @@ impl<'a> MaybeOwned<'a> {
581598
}
582599

583600
/// Returns `true` if this `MaybeOwned` wraps a borrowed string
601+
///
602+
/// # Example
603+
///
604+
/// ```rust
605+
/// let string = "orange";
606+
/// let maybe_owned_string = string.as_slice().into_maybe_owned();
607+
/// assert_eq!(true, maybe_owned_string.is_slice());
608+
/// ```
584609
#[inline]
585610
pub fn is_slice(&self) -> bool {
586611
match *self {
@@ -596,18 +621,40 @@ pub trait IntoMaybeOwned<'a> {
596621
fn into_maybe_owned(self) -> MaybeOwned<'a>;
597622
}
598623

624+
/// # Example
625+
///
626+
/// ```rust
627+
/// let owned_string = String::from_str("orange");
628+
/// let maybe_owned_string = owned_string.into_maybe_owned();
629+
/// assert_eq!(true, maybe_owned_string.is_owned());
630+
/// ```
599631
impl<'a> IntoMaybeOwned<'a> for String {
600632
#[inline]
601633
fn into_maybe_owned(self) -> MaybeOwned<'a> {
602634
Owned(self)
603635
}
604636
}
605637

638+
/// # Example
639+
///
640+
/// ```rust
641+
/// let string = "orange";
642+
/// let maybe_owned_str = string.as_slice().into_maybe_owned();
643+
/// assert_eq!(false, maybe_owned_str.is_owned());
644+
/// ```
606645
impl<'a> IntoMaybeOwned<'a> for &'a str {
607646
#[inline]
608647
fn into_maybe_owned(self) -> MaybeOwned<'a> { Slice(self) }
609648
}
610649

650+
/// # Example
651+
///
652+
/// ```rust
653+
/// let str = "orange";
654+
/// let maybe_owned_str = str.as_slice().into_maybe_owned();
655+
/// let maybe_maybe_owned_str = maybe_owned_str.into_maybe_owned();
656+
/// assert_eq!(false, maybe_maybe_owned_str.is_owned());
657+
/// ```
611658
impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> {
612659
#[inline]
613660
fn into_maybe_owned(self) -> MaybeOwned<'a> { self }

0 commit comments

Comments
 (0)