TL;DR: as
is the most common way to convert between the primitive numeric types but using it requires thinking.
fn c_to_f(c: f32) -> f32 {
(c * (9 as f32 / 5 as f32)) + 32 as f32
}
In this example though, it's more reasonable to just use floating point literals to start with:
fn c_to_f(c: f32) -> f32 {
(c * (9. / 5.)) + 32.
}
The real problem is that doing mixed type arithmetic is a bit complicated.
If you are multiplying1 a T
by a T
, you generally expect to get a result of type T
, at least with the basic types.
When mixing types, however, there are some difficulties:
- mixing signedness,
- mixing precision.
So, for example, what is the ideal result of i8 * u32
? The smallest type that can encompass the full set of all i8
and u32
values is a i64
. Should that be the result?
As another example, what is the ideal result of f32 * i32
? The smallest type that can encompass the full set of all f32
and i32
values is a f64
. Should that be the result?
I find the idea of having a such widening rather confusing. It also has performance impacts (operations on f32
can be much speedier than operations on f64
, once vectorized).
Due to those issues, Rust for now requires you to be explicit: which type do you want the computation to be carried in? Which type makes sense for your particular situation?
And then cast appropriately, using as
, and do think about which rounding mode to apply (.round()
, .ceil()
, .floor()
or .trunc()
when going from floating point to integral).
1 Adding, Subtracting and Dividing work in similar ways.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…