core::convert - Rust
Description: API documentation for the Rust `convert` mod in crate `core`.
Keywords: rust, rustlang, rust-lang, convert

# 1.0.0[−][src]Module core::convert

Traits for conversions between types.

The traits in this module provide a way to convert from one type to another type. Each trait serves a different purpose:

- Implement the `AsRef` trait for cheap reference-to-reference conversions
- Implement the `AsMut` trait for cheap mutable-to-mutable conversions
- Implement the `From` trait for consuming value-to-value conversions
- Implement the `Into` trait for consuming value-to-value conversions to types outside the current crate
- The `TryFrom` and `TryInto` traits behave like `From` and `Into`, but should be implemented when the conversion can fail.

The traits in this module are often used as trait bounds for generic functions such that to arguments of multiple types are supported. See the documentation of each trait for examples.

As a library author, you should always prefer implementing `From<T>` or `TryFrom<T>` rather than `Into<U>` or `TryInto<U>`, as `From` and `TryFrom` provide greater flexibility and offer equivalent `Into` or `TryInto` implementations for free, thanks to a blanket implementation in the standard library. Only implement `Into` or `TryInto` when a conversion to a type outside the current crate is required.

# Generic Implementations

- `AsRef` and `AsMut` auto-dereference if the inner type is a reference
- `From``<U> for T` implies `Into``<T> for U`
- `TryFrom``<U> for T` implies `TryInto``<T> for U`
- `From` and `Into` are reflexive, which means that all types can `into` themselves and `from` themselves

See each trait for usage examples.

## Enums

Infallible | The error type for errors that can never happen.

## Traits

AsMut | Used to do a cheap mutable-to-mutable reference conversion.
AsRef | Used to do a cheap reference-to-reference conversion.
From | Used to do value-to-value conversions while consuming the input value. It is the reciprocal of `Into`.
Into | A value-to-value conversion that consumes the input value. The opposite of `From`.
TryFrom | Simple and safe type conversions that may fail in a controlled way under some circumstances. It is the reciprocal of `TryInto`.
TryInto | An attempted conversion that consumes `self`, which may or may not be expensive.

## Functions

identity | The identity function.

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

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)