Skip to main content

cow_into_owned

Macro cow_into_owned 

Source
macro_rules! cow_into_owned {
    ($owned:expr, $cow:expr $(,)?) => { ... };
}
Available on crate feature alloc only.
Expand description

Converts a Cow produced from an existing owned value back into an owned value.

If the Cow is owned, this returns the owned value inside it. If the Cow is borrowed, this returns the original owned value.

This is a macro so the Cow expression can borrow from the original value before the macro moves that value in the borrowed branch.

ยงExamples

use str_utils::{cow_into_owned, ToLowercase};

let s = String::from("abc");
let ptr = s.as_ptr();
let s = cow_into_owned!(s, s.as_str().to_lowercase_cow());

assert_eq!("abc", s);
assert_eq!(ptr, s.as_ptr());