Integer division

In general, the benefit of using from instead of as where possible is that it prevents bugs when refactoring - here's a (somewhat contrived) example:

fn test(input: u8) {
    let x = input as i32;
    let y = i32::from(input);
}

What happens if I later decide to change the type of input to f32?

  • x silently gets truncated, because as can be a lossy operation.
  • y throws a compile error, as the standard library does not implement From for lossy numeric conversions.

So that's the main difference (and why Clippy nudges you to use the latter) - they don't behave differently in this case, but it makes sure that the compiler will let you know if you accidentally change the behavior.