as can do a lot of things and it’s good to know how often it is used in differrent contexts.
I particular, the numbers would be useful as a basis for the discussion about Implicit widening, polymorphic indexing, and similar ideas.
So, here’s some statistics on usage of the operator
as in the Rust codebase.
(The data itself can be found here.)
Total number of occurrences of the operator
as usize: 436
usize(uint) not in context of indexing, usually widening.
as *const/mut T: 405
Casts to raw pointers.
as u64/i64: 322
Widening casts to
u64/i64 to represent big numbers, often from usize(uint).
Sign changing casts
u64 <-> i64
as libc::*/c_*/*_t/DWORD etc: 263
Casts in FFI context.
as u32/i32: 195
Why do people cast numbers to
i32? Who knows.
use x as y;: 183
crate imports with renaming.
as i8/u8/i16/u16: 181
Casts to small integral types, predominantly narrowing.
as isize: 164
isize(int), mostly indexes/sizes to offsets.
as $T: 131
Conversions in macros
x as $T, whatever T means.
Lots of tests in libstd/num.
a[i as usize]: 91
usize(uint) directly in the indexing context.
as &Trait/&mut Trait/Box<Trait>: 62
Casts to trait objects.
as char: 51
Interpreting bytes as characters.
as Something: 48
as f64/f32: 46
Conversions to floating point numbers, from integers and from each other.
<T as Trait>: 38
Universal function call syntax.