Expand description
Cast between types, rename an import, or qualify paths to associated items.
§Type casting
as is most commonly used to turn primitive types into other primitive types, but it has other
uses that include turning pointers into addresses, addresses into pointers, and pointers into
other pointers.
let thing1: u8 = 89.0 as u8;
assert_eq!('B' as u32, 66);
assert_eq!(thing1 as char, 'Y');
let thing2: f32 = thing1 as f32 + 10.5;
assert_eq!(true as u8 + thing2 as u8, 100);In general, any cast that can be performed via ascribing the type can also be done using as,
so instead of writing let x: u32 = 123, you can write let x = 123 as u32 (note: let x: u32 = 123 would be best in that situation). The same is not true in the other direction, however;
explicitly using as allows a few more coercions that aren’t allowed implicitly, such as
changing the type of a raw pointer or turning closures into raw pointers.
as can be seen as the primitive for From and Into: as only works with primitives
(u8, bool, str, pointers, …) whereas From and Into also works with types like
String or Vec.
as can also be used with the _ placeholder when the destination type can be inferred. Note
that this can cause inference breakage and usually such code should use an explicit type for
both clarity and stability. This is most useful when converting pointers using as *const _ or
as *mut _ though the cast method is recommended over as *const _ and it is
the same for as *mut _: those methods make the intent clearer.
§Renaming imports
as is also used to rename imports in use and extern crate statements:
use std::{mem as memory, net as network};
// Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.§Qualifying paths
You’ll also find with From and Into, and indeed all traits, that as is used for the
fully qualified path, a means of disambiguating associated items, i.e. functions,
constants, and types. For example, if you have a type which implements two traits with identical
method names (e.g. Into::<u32>::into and Into::<u64>::into), you can clarify which method
you’ll use with <MyThing as Into<u32>>::into(my_thing)1. This is quite verbose,
but fortunately, Rust’s type inference usually saves you from needing this, although it is
occasionally necessary, especially with methods that return a generic type like Into::into or
methods that don’t take self. It’s more common to use in macros where it can provide necessary
hygiene.
§Further reading
For more information on what as is capable of, see the Reference on type cast expressions,
renaming imported entities, renaming extern crates
and qualified paths.
You should probably never use this syntax with
Intoand instead writeT::from(my_thing). It just happens that there aren’t any great examples for this syntax in the standard library. Also, at time of writing, the compiler tends to suggest fully-qualified paths to fix ambiguousInto::intocalls, so the example should hopefully be familiar. ↩