# Trait std::borrow::Borrow 1.0.0 [−] [src]

    pub trait Borrow<Borrowed> where
        Borrowed: ?Sized, {
        fn borrow(&self) -> &Borrowed;
    }

A trait for borrowing data.

In general, there may be several ways to "borrow" a piece of data. The typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T` (a mutable borrow). But types like `Vec<T>` provide additional kinds of borrows: the borrowed slices `&[T]` and `&mut [T]`.

When writing generic code, it is often desirable to abstract over all ways of borrowing data from a given type. That is the role of the `Borrow` trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given type can be borrowed as multiple different types. In particular, `Vec<T>: Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.

If you are implementing `Borrow` and both `Self` and `Borrowed` implement `Hash`, `Eq`, and/or `Ord`, they must produce the same result.

`Borrow` is very similar to, but different than, `AsRef`. See the book for more.

## Required Methods

ⓘImportant traits for &'a mut I

### Important traits for &'a mut I

`impl<'a, I> Iterator for &'a mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`

### `fn borrow(&self) -> &Borrowed`

Immutably borrows from an owned value.

# Examples

    use std::borrow::Borrow;

    fn check<T: Borrow<str>>(s: T) {
        assert_eq!("Hello", s.borrow());
    }

    let s = "Hello".to_string();

    check(s);

    let s = "Hello";

    check(s);Run

## Implementors

- [src]
  `impl<T> Borrow<T> for T where
  T: ?Sized,`
- [src]
  `impl<T> Borrow<[T]> for [T; 9]`
- [src]
  `impl<T> Borrow<[T]> for [T; 31]`
- [src]
  `impl<T> Borrow<[T]> for [T; 24]`
- [src]
  `impl<T> Borrow<[T]> for [T; 7]`
- [src]
  `impl<T> Borrow<[T]> for [T; 21]`
- [src]
  `impl<T> Borrow<[T]> for [T; 8]`
- [src]
  `impl<T> Borrow<[T]> for [T; 10]`
- [src]
  `impl<T> Borrow<[T]> for [T; 13]`
- [src]
  `impl<T> Borrow<[T]> for [T; 23]`
- [src]
  `impl<T> Borrow<[T]> for [T; 0]`
- [src]
  `impl<T> Borrow<[T]> for [T; 27]`
- [src]
  `impl<T> Borrow<[T]> for [T; 25]`
- [src]
  `impl<T> Borrow<[T]> for [T; 17]`
- [src]
  `impl<T> Borrow<[T]> for [T; 28]`
- [src]
  `impl<T> Borrow<[T]> for [T; 29]`
- [src]
  `impl<T> Borrow<[T]> for [T; 19]`
- [src]
  `impl<T> Borrow<[T]> for [T; 26]`
- [src]
  `impl<T> Borrow<[T]> for [T; 20]`
- [src]
  `impl<T> Borrow<[T]> for [T; 1]`
- [src]
  `impl<T> Borrow<[T]> for [T; 11]`
- [src]
  `impl<T> Borrow<[T]> for [T; 32]`
- [src]
  `impl<T> Borrow<[T]> for [T; 14]`
- [src]
  `impl<T> Borrow<[T]> for [T; 12]`
- [src]
  `impl<T> Borrow<[T]> for [T; 15]`
- [src]
  `impl<T> Borrow<[T]> for [T; 4]`
- [src]
  `impl<T> Borrow<[T]> for [T; 16]`
- [src]
  `impl<'a, T> Borrow<T> for &'a mut T where
  T: ?Sized,`
- [src]
  `impl<T> Borrow<[T]> for [T; 30]`
- [src]
  `impl<T> Borrow<[T]> for [T; 18]`
- [src]
  `impl<T> Borrow<[T]> for [T; 6]`
- [src]
  `impl<T> Borrow<[T]> for [T; 2]`
- [src]
  `impl<T> Borrow<[T]> for [T; 22]`
- [src]
  `impl<T> Borrow<[T]> for [T; 3]`
- [src]
  `impl<'a, T> Borrow<T> for &'a T where
  T: ?Sized,`
- [src]
  `impl<T> Borrow<[T]> for [T; 5]`
- [src]
  `impl<T> Borrow<T> for Box<T> where
  T: ?Sized,`
- [src]
  `impl<T> Borrow<T> for Arc<T> where
  T: ?Sized,`
- [src]
  `impl<'a, B> Borrow<B> for Cow<'a, B> where
  B: ToOwned + ?Sized,
  <B as ToOwned>::Owned: 'a,`
- [src]
  `impl Borrow<str> for String`
- [src]
  `impl<T> Borrow<T> for Rc<T> where
  T: ?Sized,`
- [src]
  `impl<T> Borrow<[T]> for Vec<T>`
- [src]
  `impl Borrow<CStr> for CString`
- [src]
  `impl Borrow<OsStr> for OsString`
- [src]
  `impl Borrow<Path> for PathBuf`

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)