Problem
when running this rust snippet code
|
|
we expect to have result 0.3. But the acual we got like
|
|
Solution
Use package Decimal
to solve this, import Decimal pkg (https://docs.rs/rust_decimal/latest/rust_decimal). On the Cargo.toml, put
|
|
and change the main func to
|
|
or
|
|
run again the code, and then we can see the correct result 0.3
Use Macros
we also can use macros (https://docs.rs/rust_decimal_macros/latest/rust_decimal_macros/) for nice syntax.
run cargo add rust_decimal_macros or add it to the cargo.toml like previous,
then change the main function to this:
|
|
Note
unwarp()is a quick way to extract the value, but it will panic if the value is not present.- use
unwrap_or_default()for safer way