# Trait collections::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

### `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);

## Implementations on Foreign Types

### `impl<T> Borrow<T> for Rc<T> where
T: ?Sized,`
[src]

#### `fn borrow(&self) -> &T`
[src]

### `impl<T> Borrow<T> for Box<T> where
T: ?Sized,`
[src]

#### `fn borrow(&self) -> &T`
[src]

### `impl<T> Borrow<T> for Arc<T> where
T: ?Sized,`
[src]

#### `fn borrow(&self) -> &T`
[src]

## Implementors

- `impl<'a, B> Borrow<B> for Cow<'a, B> where
  B: ToOwned + ?Sized,
  <B as ToOwned>::Owned: 'a,`
- `impl<T> Borrow<[T]> for Vec<T>`
- `impl Borrow<str> for String`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8652) => core/5e4b76f::array[0]::{{impl}}[168]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:9179) => core/5e4b76f::array[0]::{{impl}}[355]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8621) => core/5e4b76f::array[0]::{{impl}}[157]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8776) => core/5e4b76f::array[0]::{{impl}}[212]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:9117) => core/5e4b76f::array[0]::{{impl}}[333]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8869) => core/5e4b76f::array[0]::{{impl}}[245]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:9024) => core/5e4b76f::array[0]::{{impl}}[300]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8714) => core/5e4b76f::array[0]::{{impl}}[190]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8900) => core/5e4b76f::array[0]::{{impl}}[256]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8993) => core/5e4b76f::array[0]::{{impl}}[289]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8559) => core/5e4b76f::array[0]::{{impl}}[135]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8590) => core/5e4b76f::array[0]::{{impl}}[146]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:9086) => core/5e4b76f::array[0]::{{impl}}[322]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8249) => core/5e4b76f::array[0]::{{impl}}[25]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8218) => core/5e4b76f::array[0]::{{impl}}[14]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8745) => core/5e4b76f::array[0]::{{impl}}[201]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8280) => core/5e4b76f::array[0]::{{impl}}[36]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8962) => core/5e4b76f::array[0]::{{impl}}[278]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8404) => core/5e4b76f::array[0]::{{impl}}[80]::{{initializer}}[0] }, Slice([])) }]`
- `impl<'a, T> Borrow<T> for &'a T where
  T: ?Sized,`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8497) => core/5e4b76f::array[0]::{{impl}}[113]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8187) => core/5e4b76f::array[0]::{{impl}}[3]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<T> for T where
  T: ?Sized,`
- `impl<'a, T> Borrow<T> for &'a mut T where
  T: ?Sized,`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:9055) => core/5e4b76f::array[0]::{{impl}}[311]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8373) => core/5e4b76f::array[0]::{{impl}}[69]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8311) => core/5e4b76f::array[0]::{{impl}}[47]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:9148) => core/5e4b76f::array[0]::{{impl}}[344]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8931) => core/5e4b76f::array[0]::{{impl}}[267]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8807) => core/5e4b76f::array[0]::{{impl}}[223]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8435) => core/5e4b76f::array[0]::{{impl}}[91]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8838) => core/5e4b76f::array[0]::{{impl}}[234]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8342) => core/5e4b76f::array[0]::{{impl}}[58]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8683) => core/5e4b76f::array[0]::{{impl}}[179]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8466) => core/5e4b76f::array[0]::{{impl}}[102]::{{initializer}}[0] }, Slice([])) }]`
- `impl<T> Borrow<[T]> for [T; Const { ty: usize, val: Unevaluated(DefId { krate: CrateNum(1), node: DefIndex(1:8528) => core/5e4b76f::array[0]::{{impl}}[124]::{{initializer}}[0] }, Slice([])) }]`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand 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`)