Trait artifact_app::dev_prefix::AsRef1.0.0 [] [src]

pub trait AsRef<T> where T: ?Sized {
    fn as_ref(&self) -> &T;
}

A cheap, reference-to-reference conversion.

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

Note: this trait must not fail. If the conversion can fail, use a dedicated method which returns an Option<T> or a Result<T, E>.

Examples

Both String and &str implement AsRef<str>:

fn is_hello<T: AsRef<str>>(s: T) {
   assert_eq!("hello", s.as_ref());
}

let s = "hello";
is_hello(s);

let s = "hello".to_string();
is_hello(s);

Generic Impls

  • AsRef auto-dereferences if the inner type is a reference or a mutable reference (e.g.: foo.as_ref() will work the same if foo has type &mut Foo or &&mut Foo)

Required Methods

Performs the conversion.

Trait Implementations

impl<T> Trait for AsRef<T> + 'static + Send where T: ?Sized

impl<T> Trait for AsRef<T> + 'static + Sync where T: ?Sized

impl<T> Trait for AsRef<T> + 'static + Send + Sync where T: ?Sized

Implementors