Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an &i32 to f64?

Tags:

rust

I'm trying to solve an exercise at the end of this chapter in the Rust Book.

Here is a code sample:

fn mean(v: &Vec<i32>) -> f64 {
    let mut sum = 0.0;
    let mut count = 0.0;

    for val in v {
        sum += &f64::from(val);
        count += 1.0;
    }

    sum / count
}

fn main() {
    let v = vec![1, 2, 3, 4];

    println!("The mean is {}", mean(&v));
}

The error is:

error[E0277]: the trait bound `f64: std::convert::From<&i32>` is not satisfied
 --> src/main.rs:6:17
  |
6 |         sum += &f64::from(val);
  |                 ^^^^^^^^^ the trait `std::convert::From<&i32>` is not implemented for `f64`
  |
  = help: the following implementations were found:
            <f64 as std::convert::From<f32>>
            <f64 as std::convert::From<i16>>
            <f64 as std::convert::From<i32>>
            <f64 as std::convert::From<i8>>
          and 3 others
  = note: required by `std::convert::From::from`

I also tried using the as keyword but it didn't help.

like image 333
Artsiom Shamsutdzinau Avatar asked Mar 09 '19 17:03

Artsiom Shamsutdzinau


People also ask

How do you convert in Word?

Follow these easy steps to turn a PDF into a Microsoft Word document: Click the Select a file button above or drag and drop a PDF into the drop zone. Select the PDF you want to convert to the DOCX file format. Watch Acrobat automatically convert the file from PDF to Word document.

How do you convert a document?

Click the File tab. Do one of the following: To convert the document without saving a copy, click Info, and then click Convert. To create a new copy of the document in Word 2016 or Word 2013 mode, click Save As, and then choose the location and the folder where you want to save the new copy.

How do I convert an image to PDF?

Drag and drop an image file (JPG, PNG, BMP, and more) to use our PDF converter. Drag and drop an image file (JPG, PNG, BMP, and more) to use our PDF converter. Drag and drop an image file (JPG, PNG, BMP, and more) to convert to PDF. Your file will be securely uploaded to Adobe cloud storage.


2 Answers

f64 only implements From for i32, not &i32 (which is a reference to an i32). To get this to work, you will need to dereference val.

fn mean(v: &Vec<i32>) -> f64 {
    let mut sum = 0.0;
    let mut count = 0.0;

    for val in v {
        sum += f64::from(*val);
        count += 1.0;
    }

    sum / count
}

The same applies if you try to do val as f64, and in fact, you get a much more helpful error message in that case:

error[E0606]: casting `&i32` as `f64` is invalid
 --> src/main.rs:6:16
  |
6 |         sum += val as f64;
  |                ---^^^^^^^
  |                |
  |                cannot cast `&i32` as `f64`
  |                help: dereference the expression: `*val`
like image 184
Joe Clay Avatar answered Nov 02 '22 22:11

Joe Clay


You can dereference the variable with *val as f64.

fn mean(v: &Vec<i32>) -> f64 {
    let mut sum = 0.0;
    let mut count = 0.0;

    for val in v {
        sum += *val as f64;
        count += 1.0;
    }

    sum / count
}

fn main() {
    let v = vec![1, 2, 3, 4];

    println!("The mean is {}", mean(&v));
}

Another way to do it

fn main() {
    let v = vec![1, 2, 3, 4];
    let mean: f64 = v.iter().map(|&val| val as f64).sum::<f64>() / v.len() as f64;

    println!("The mean is {}", mean);
}
like image 28
Ender Fletcher Avatar answered Nov 02 '22 22:11

Ender Fletcher