Trait TakeString

Source
pub trait TakeString {
    // Required methods
    fn as_str(&self) -> &str;
    fn take(self) -> String;
}
Expand description

The TakeString trait for Rust enables treating &str and String instances interchangeably.

§Examples

take_string can take in instances of both String and &str. If it takes in a &str, no copying is needed unless take is called.

use take_ref::TakeString;
 
fn take_string(value: impl TakeString) {
    assert_eq!(value.as_str(), "Hi!"); // `as_str` references the string in place.
    assert_eq!(value.as_str(), "Hi!"); // `as_str` can be repeated until `take` is called.
    assert_eq!(value.take(), "Hi!"); // `take` consumes the value.
}
 
take_string("Hi!");
 
let s = "Hi!";
take_string(s);

§Disallowed Operations

ref_taken fails to compile since it attempts to reference value after take has already consumed it.

fn ref_taken(value: impl TakeString) {
    value.take(); // `take` consumes the value.
    value.as_str(); // This call is disallowed since the value has already been consumed.
}

take_taken fails to compile since it attempts to take value after take has already consumed it.

fn take_taken(value: impl TakeString) {
    value.take(); // `take` consumes the value.
    value.take(); // This call is disallowed since the value has already been consumed.
}

Required Methods§

Source

fn as_str(&self) -> &str

Reference the string as a &str.

Source

fn take(self) -> String

Take ownership of the String or construct a String and drop self.

Implementations on Foreign Types§

Source§

impl TakeString for &str

Source§

fn as_str(&self) -> &str

Reference the string as a &str.

Source§

fn take(self) -> String

Construct a String and drop self.

Source§

impl TakeString for String

Source§

fn as_str(&self) -> &str

Reference the string as a &str.

Source§

fn take(self) -> String

Take ownership of the String and drop self.

Implementors§