0

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=67620c5e1ce288f7ed0ffdefea9bcf68

fn main() {
    let a: usize = 100;
    let b: u32 = 100;
    let z: i32 = a*b;
}

How do I tell the compiler which type I want there?

Pablo Tato Ramos
  • 409
  • 4
  • 18

1 Answers1

1

How do I tell the compiler which type I want there?

You cast the variable to proper type. For example if you want to do the multiplication as i32 type, you would:

let z: i32 = (a as i32) * (b as i32);

More about casting is available in Rust manual - Casting.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Thank you. Why does the compiler suggest `try_into()` if this is simpler? – Pablo Tato Ramos Feb 27 '20 at 16:52
  • Because conversions with `as` may panic (in debug mode) or cause wrong results (in release mode) if the inputs don't fit in the requested type. – Jmb Feb 28 '20 at 08:19