mirror of https://github.com/rust-lang/rust.git
Add documentation for string->Cow conversions
Mostly, it's just to reassure everyone that these functions don't allocate. Part of #51430
This commit is contained in:
parent
8fd946c63a
commit
69a37a63fa
|
@ -2352,6 +2352,16 @@ impl<'a> From<Cow<'a, str>> for String {
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<'a> From<&'a str> for Cow<'a, str> {
|
impl<'a> From<&'a str> for Cow<'a, str> {
|
||||||
|
/// Converts a string slice into a Borrowed variant.
|
||||||
|
/// No heap allocation is performed, and the string
|
||||||
|
/// is not copied.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
|
/// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));
|
||||||
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(s: &'a str) -> Cow<'a, str> {
|
fn from(s: &'a str) -> Cow<'a, str> {
|
||||||
Cow::Borrowed(s)
|
Cow::Borrowed(s)
|
||||||
|
@ -2360,6 +2370,18 @@ impl<'a> From<&'a str> for Cow<'a, str> {
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<'a> From<String> for Cow<'a, str> {
|
impl<'a> From<String> for Cow<'a, str> {
|
||||||
|
/// Converts a String into an Owned variant.
|
||||||
|
/// No heap allocation is performed, and the string
|
||||||
|
/// is not copied.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
|
/// let s = "eggplant".to_string();
|
||||||
|
/// let s2 = "eggplant".to_string();
|
||||||
|
/// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));
|
||||||
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(s: String) -> Cow<'a, str> {
|
fn from(s: String) -> Cow<'a, str> {
|
||||||
Cow::Owned(s)
|
Cow::Owned(s)
|
||||||
|
@ -2368,6 +2390,17 @@ impl<'a> From<String> for Cow<'a, str> {
|
||||||
|
|
||||||
#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
|
#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
|
||||||
impl<'a> From<&'a String> for Cow<'a, str> {
|
impl<'a> From<&'a String> for Cow<'a, str> {
|
||||||
|
/// Converts a String reference into a Borrowed variant.
|
||||||
|
/// No heap allocation is performed, and the string
|
||||||
|
/// is not copied.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
|
/// let s = "eggplant".to_string();
|
||||||
|
/// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));
|
||||||
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(s: &'a String) -> Cow<'a, str> {
|
fn from(s: &'a String) -> Cow<'a, str> {
|
||||||
Cow::Borrowed(s.as_str())
|
Cow::Borrowed(s.as_str())
|
||||||
|
|
Loading…
Reference in New Issue