Movatterモバイル変換


[0]ホーム

URL:


as

Keywordas 

Source
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 otheruses that include turning pointers into addresses, addresses into pointers, and pointers intoother pointers.

letthing1: u8 =89.0asu8;assert_eq!('B'asu32,66);assert_eq!(thing1aschar,'Y');letthing2: f32 = thing1asf32 +10.5;assert_eq!(trueasu8 + thing2asu8,100);

In general, any cast that can be performed via ascribing the type can also be done usingas,so instead of writinglet x: u32 = 123, you can writelet 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 usingas allows a few more coercions that aren’t allowed implicitly, such aschanging the type of a raw pointer or turning closures into raw pointers.

as can be seen as the primitive forFrom andInto:as only works with primitives(u8,bool,str, pointers, …) whereasFrom andInto also works with types likeString orVec.

as can also be used with the_ placeholder when the destination type can be inferred. Notethat this can cause inference breakage and usually such code should use an explicit type forboth clarity and stability. This is most useful when converting pointers usingas *const _ oras *mut _ though thecast method is recommended overas *const _ and it isthe same foras *mut _: those methods make the intent clearer.

§Renaming imports

as is also used to rename imports inuse andextern crate statements:

usestd::{memasmemory, netasnetwork};// Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.

§Qualifying paths

You’ll also find withFrom andInto, and indeed all traits, thatas is used for thefully 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 identicalmethod names (e.g.Into::<u32>::into andInto::<u64>::into), you can clarify which methodyou’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 isoccasionally necessary, especially with methods that return a generic type likeInto::into ormethods that don’t takeself. It’s more common to use in macros where it can provide necessaryhygiene.

§Further reading

For more information on whatas is capable of, see the Reference ontype cast expressions,renaming imported entities,renamingextern cratesandqualified paths.


  1. You should probably never use this syntax withInto and instead writeT::from(my_thing). It just happens that there aren’t any great examples for this syntax inthe standard library. Also, at time of writing, the compiler tends to suggest fully-qualifiedpaths to fix ambiguousInto::into calls, so the example should hopefully be familiar. 


[8]ページ先頭

©2009-2026 Movatter.jp