There are two different categories of numbers in Rust: integers (which can be signed or unsigned) and floating-point numbers.
The name of a numeric type consists of two parts:
The following combinations are possible:
u8
, i8
u16
, i16
u32
, i32
, f32
u64
, i64
, f64
u128
, i128
Note that there are only 32-bits and 64-bits variants for floating-point numbers.
Rust doesn't do any implicit type conversion.
This means that if you need to turn one numeric type into another, you have to do so explicitly.
When converting from a larger type to a smaller one (for instance u64
to u32
) you could lose data.
Converting from a floating point to an integer will lose everything behind the decimal point, effectively rounding down.
In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from 0
(off) to 10
(maximum).
At its lowest speed (1
), 221
cars are produced each hour. The production increases linearly with the speed. So with the speed set to 4
, it should produce 4 * 221 = 884
cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:
1
to 4
: 100% success rate.5
to 8
: 90% success rate.9
and 10
: 77% success rate.You have two tasks.
Implement a method to calculate the assembly line's production rate per hour, taking into account its success rate:
assembly_line::production_rate_per_hour(6)
// Returns: 1193.4
Note that the value returned is an f64
.
Implement a method to calculate how many working cars are produced per minute:
assembly_line::working_items_per_minute(6)
// Returns: 19
Note that the value returned is an u32
.
Sign up to Exercism to learn and master Rust with 98 exercises, and real human mentoring, all for free.