E0100
Conversion that can fail, in a constant
ErrorSafe fixA constant's value cannot use u8(x), char(x), or i64(x) of an f64. Each gives an optional, which is None when the value does not fit, and a constant holds no optional. Write the literal with its type instead: const B: u8 = 200. A conversion that cannot fail, i64(x) of a u8 or a char, or f64(n), is allowed.
Why
A constant's value is known before the program runs, so whether the conversion fits is known too, and the literal says it directly. When the conversion is the whole value and its argument fits, the safe fix writes that literal, and the type when the constant declares none. A char is written as a literal only when it needs no escape, and an f64 only when it is a whole number in i64's range; any other value is left to you.
Broken
const LIMIT = u8(200)
fn main() {
print(LIMIT)
}
What sisu says
error[E0100]: a constant cannot use a conversion that can fail --> E0100.sisu:1:15 | 1 | const LIMIT = u8(200) | ^^^^^^^ `u8(...)` gives a `u8?` | = help: write the literal with its type: `const B: u8 = 200` help: write the literal with its type | 1 - const LIMIT = u8(200) 1 + const LIMIT: u8 = 200 | sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0100
Fixed
const LIMIT: u8 = 200
fn main() {
print(LIMIT)
}
Sisu