Rust エラー「error[E0277]: cannot add a float to an integer」が発生した場合の対処法

Rust エラー「error[E0277]: cannot add a float to an integer」が発生した場合の対処法

Rustで、エラー「error[E0277]: cannot add a float to an integer」が発生した場合の対処法を記述してます。「integer」と「float」で演算した際に発生します。Rustのバージョンは1.64.0を使用してます。

環境

  • OS windows11 home
  • rustc 1.64.0

エラー全文

以下のコードで発生

fn main() {

    println!( "{}",  5 + 2.5 );
    println!( "{}",  5 - 2.5 );
    println!( "{}",  5 * 2.5 );
    println!( "{}",  5 / 2.5 );
    println!( "{}",  5 % 2.5 );

}

エラー全文

error[E0277]: cannot add a float to an integer
 --> test.rs:3:24
  |
3 |     println!( "{}",  5 + 2.5 );
  |                        ^ no implementation for `{integer} + {float}`
  |
  = help: the trait `Add<{float}>` is not implemented for `{integer}`
  = help: the following other types implement trait `Add<Rhs>`:
            <&'a f32 as Add<f32>>
            <&'a f64 as Add<f64>>
            <&'a i128 as Add<i128>>
            <&'a i16 as Add<i16>>
            <&'a i32 as Add<i32>>
            <&'a i64 as Add<i64>>
            <&'a i8 as Add<i8>>
            <&'a isize as Add<isize>>
          and 48 others

error[E0277]: cannot subtract `{float}` from `{integer}`
 --> test.rs:4:24
  |
4 |     println!( "{}",  5 - 2.5 );
  |                        ^ no implementation for `{integer} - {float}`
  |
  = help: the trait `Sub<{float}>` is not implemented for `{integer}`
  = help: the following other types implement trait `Sub<Rhs>`:
            <&'a f32 as Sub<f32>>
            <&'a f64 as Sub<f64>>
            <&'a i128 as Sub<i128>>
            <&'a i16 as Sub<i16>>
            <&'a i32 as Sub<i32>>
            <&'a i64 as Sub<i64>>
            <&'a i8 as Sub<i8>>
            <&'a isize as Sub<isize>>
          and 48 others

error[E0277]: cannot multiply `{integer}` by `{float}`
 --> test.rs:5:24
  |
5 |     println!( "{}",  5 * 2.5 );
  |                        ^ no implementation for `{integer} * {float}`
  |
  = help: the trait `Mul<{float}>` is not implemented for `{integer}`
  = help: the following other types implement trait `Mul<Rhs>`:
            <&'a f32 as Mul<f32>>
            <&'a f64 as Mul<f64>>
            <&'a i128 as Mul<i128>>
            <&'a i16 as Mul<i16>>
            <&'a i32 as Mul<i32>>
            <&'a i64 as Mul<i64>>
            <&'a i8 as Mul<i8>>
            <&'a isize as Mul<isize>>
          and 49 others

error[E0277]: cannot divide `{integer}` by `{float}`
 --> test.rs:6:24
  |
6 |     println!( "{}",  5 / 2.5 );
  |                        ^ no implementation for `{integer} / {float}`
  |
  = help: the trait `Div<{float}>` is not implemented for `{integer}`
  = help: the following other types implement trait `Div<Rhs>`:
            <&'a f32 as Div<f32>>
            <&'a f64 as Div<f64>>
            <&'a i128 as Div<i128>>
            <&'a i16 as Div<i16>>
            <&'a i32 as Div<i32>>
            <&'a i64 as Div<i64>>
            <&'a i8 as Div<i8>>
            <&'a isize as Div<isize>>
          and 54 others

error[E0277]: cannot mod `{integer}` by `{float}`
 --> test.rs:7:24
  |
7 |     println!( "{}",  5 % 2.5 );
  |                        ^ no implementation for `{integer} % {float}`
  |
  = help: the trait `Rem<{float}>` is not implemented for `{integer}`
  = help: the following other types implement trait `Rem<Rhs>`:
            <&'a f32 as Rem<f32>>
            <&'a f64 as Rem<f64>>
            <&'a i128 as Rem<i128>>
            <&'a i16 as Rem<i16>>
            <&'a i32 as Rem<i32>>
            <&'a i64 as Rem<i64>>
            <&'a i8 as Rem<i8>>
            <&'a isize as Rem<isize>>
          and 54 others

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0277`.

原因

型の違う値同士で演算しているため

対処法

「型」を合わせる

fn main() {

    println!( "{}",  f64::from(5) + 2.5 ); 
    println!( "{}",  f64::from(5) - 2.5 );
    println!( "{}",  f64::from(5) * 2.5 );
    println!( "{}",  f64::from(5) / 2.5 );
    println!( "{}",  f64::from(5) % 2.5 );

}

実行結果