Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

How to fix rust operations not working as expected?

I have implemented a simple command-line calculator in Rust. The add function acts as normal but the subtract, multiply, and divide functions don't work. The rest of the code is on GitHub: https://github.com/henryboisdequin/rust-calculator.

calc.rs

impl Calc {
    pub fn add(arr: Vec<i64>) -> f64 {
        let mut total: f64 = 0.0;

        for num in arr {
            total += num as f64;
        }

        total
    }

    pub fn sub(arr: Vec<i64>) -> f64 {
        let mut total: f64 = 0.0;

        for num in arr {
            total -= num as f64;
        }

        total
    }

    pub fn mul(arr: Vec<i64>) -> f64 {
        let mut total: f64 = 0.0;

        for num in arr {
            total *= num as f64;
        }

        total
    }

    pub fn div(arr: Vec<i64>) -> f64 {
        let mut total: f64 = 0.0;

        for num in arr {
            total /= num as f64;
        }

        total
    }
}
question from:https://stackoverflow.com/questions/65649650/how-to-fix-rust-operations-not-working-as-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of having your functions take Vec<i64>, I would instead suggest &[i64], or even &[f64] to avoid the as f64. This wouldn't really break your existing code, as you can just borrow a Vec<i64>, to have it auto dereference into &[i64].

You can simplify add() by using sum(), and mul() by using product().

pub fn add(arr: &[i64]) -> f64 {
    arr.iter().map(|&x| x as f64).sum()
}

pub fn mul(arr: &[i64]) -> f64 {
    arr.iter().map(|&x| x as f64).product()
}

You can similarly simplify sub() and div() with next() and then fold().

pub fn sub(arr: &[i64]) -> f64 {
    let mut it = arr.iter().map(|&x| x as f64);
    it.next()
        .map(|x| it.fold(x, |acc, x| acc - x))
        .unwrap_or(0.0)
}

pub fn div(arr: &[i64]) -> f64 {
    let mut it = arr.iter().map(|&x| x as f64);
    it.next()
        .map(|x| it.fold(x, |acc, x| acc / x))
        .unwrap_or(0.0)
}

You can even simplify them further, by using fold_first(). However that is currently experimental and nightly only. Instead you can use fold1() from the itertools crate, or reduce() from the reduce crate.

// itertools = "0.10"
use itertools::Itertools;

pub fn sub(arr: &[i64]) -> f64 {
    arr.iter().map(|&x| x as f64).fold1(|a, b| a - b).unwrap_or(0.0)
}

pub fn div(arr: &[i64]) -> f64 {
    arr.iter().map(|&x| x as f64).fold1(|a, b| a / b).unwrap_or(0.0)
}

You can even replace the closures with Sub::sub and Div::div.

// itertools = "0.10"
use itertools::Itertools;
use std::ops::{Div, Sub};

pub fn sub(arr: &[i64]) -> f64 {
    arr.iter().map(|&x| x as f64).fold1(Sub::sub).unwrap_or(0.0)
}

pub fn div(arr: &[i64]) -> f64 {
    arr.iter().map(|&x| x as f64).fold1(Div::div).unwrap_or(0.0)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...