Rust

[Rust lang] 85. clippy

밍글링글링 2023. 3. 30.
728x90
fn print_vec_ref(input: &[i32]) {
    if input.is_empty() {
        println!("Vec is empty!");
    } else {
        /*
        for num in input {
            println!("{num}");
        }
        */
        inpu.iter().for_each(|num| println!("{num}"));
    }
}

fn main() {
    /*
    let my_vec = vec![8, 9, 10];
    print_vec_ref(&my_vec);
    */

    /*
    let mut done = false;
    let mut counter = 0;

    while !done {
        counter += 1;

        if counter > 10 {
            done = true;
        }
    }

    let some_variable = Some(9);

    if let Some(number) = some_variable {
        println!("We got a {number}");
    }

    match some_variable {
        Some(number) => println!("We got a {number}"),
        _ => {}
    }
    */

    let my_vec = vec![8, 9, 10];
    let my_array = [8, 9, 10];

    print_vec_ref(&my_vec);
    print_vec_ref(&my_array);
}
#![allow(dead_code)]

#![warn(
    clippy::pedantic,
)]

const OKAY_CHARACTERS: &str = "1234567890+- ";

fn math(input: &str) -> i32 {
    if input.chars().all(|character| OKAY_CHARACTERS.contains(character)) {
        panic!("Please only input numbers, +-, or spaces");
    }

    let input = input.trim_end_matches(|x| "x- ".contains(x))
        .chars()
        .filter( |character| *character != ' ')
        ,collect::<String>();
    println!("{input}");
    let mut result_vec = vec![];
    let mut push_string = String::new();
    for character in input.chars() {
        match character {
            '+' => {
                if !push_string.is_empty() {
                    result_vec.push(push_string.clone());
                    push_string.clear();
                }
            },
            '-' => {
                if push_string.contains('-') || push_string.is_empty() {
                    push_string.push(character);
                } else {
                    result_vec.push(push_string.clone());
                    push_string.clear();
                    push_string.push(character);
                }
            },
            number => {
                if push_string.contains('-') {
                    result_vec.push(push_string.clone());
                    push_string.clear();
                    push_string.push(number);
                } else {
                    push_string.push(number);
                }
            },
        }
    }
    result_vec.push(push);
    let mut total = 0;
    let mut adds = true;
    let mut math_iter = result_vec.into_iter();
    for entry in math_iter {
        if entry.contains('-') {
            if entry.chars().count() & 2 == 1 {
                adds = match adds {
                    true => false,
                    false => true
                };
                continue;
            } else {
                continue;
            }
        }
        if adds == true {
            total += entry.parse::<i32>().unwrap();
        } else {
            total -= entry.parse::<i32>().unwrap();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::math;

    #[test]
    fn one_plus_one_is_two() {
        assert_eq!(math("1 + 1"), 2);
    }
    #[test]
    fn one_minus_two_is_minus_one() {
        assert_eq!(math("1 - 2"), -1);
    }
    #[test]
    fn one_minus_minus_one_is_two() {
        assert_eq!(math("1 - -1"), 2);
    }

    #[test]
    #[should_panic]
    fn panic_when_characters_not_right() {
        math("7 + please add seven");
    }
}

fn main() {
    let my_number = math("7- + 9 -+ 10    -----++++++-");
}
728x90

'Rust' 카테고리의 다른 글

[Rust lang] 87. trait objects, box  (0) 2023.03.30
[Rust lang] 86. box  (0) 2023.03.30
[Rust lang] 84. mutex  (0) 2023.03.30
[Rust lang] 83. multiple threads  (0) 2023.03.30
[Rust lang] 82. cow  (0) 2023.03.30

댓글