Strings
Mathematics
fn math(s: &str) -> i32 { // remove whitespace characters so all we have left are digits and the operator let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect(); // find the position of the operator character let pos: usize = math_str .chars() .position(|ch| ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=') .expect("invalid operator, expected +, -, *, / or ="); // extract the two numbers from the string let num1: i32 = math_str[..pos].parse().unwrap(); let num2: i32 = math_str[pos + 1..].parse().unwrap(); match &math_str[pos..pos + 1] { "+" => num1 + num2, "-" => num1 - num2, "*" => num1 * num2, "/" => num1 / num2, _ => (num1 == num2) as i32, } }
- fn math(s: &str) -> i32 {
// first remove whitespace characters so all we have left are digits and the operatorlet num_str: String = s.chars().filter(|ch| *ch != ' ').collect();- // remove whitespace characters so all we have left are digits and the operator
- let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect();
// extract the operator characterlet pos: usize = num_str- // find the position of the operator character
- let pos: usize = math_str
- .chars()
- .position(|ch| ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=')
.expect("invalid operator, expected '+, -, *, / or =");let operator: char = num_str.chars().nth(pos).unwrap();- .expect("invalid operator, expected +, -, *, / or =");
- // extract the two numbers from the string
let num1: i32 = num_str.get(0..pos).unwrap().parse().unwrap();let num2: i32 = num_str.get(pos + 1..).unwrap().parse().unwrap();- let num1: i32 = math_str[..pos].parse().unwrap();
- let num2: i32 = math_str[pos + 1..].parse().unwrap();
match operator {'+' => num1 + num2,'-' => num1 - num2,'*' => num1 * num2,'/' => num1 / num2,- match &math_str[pos..pos + 1] {
- "+" => num1 + num2,
- "-" => num1 - num2,
- "*" => num1 * num2,
- "/" => num1 / num2,
- _ => (num1 == num2) as i32,
- }
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_add() { assert_eq!(math("1 + 2"), 3); assert_eq!(math("12+ 3456789"), 3456801); assert_eq!(math("50 + 10 0"), 150); } #[test] fn test_sub() { assert_eq!(math("1-3"), -2); assert_eq!(math(" 500 - 150 "), 350); assert_eq!(math("0-2"), -2); } #[test] fn test_multiply() { assert_eq!(math("1* 30"), 30); assert_eq!(math("20 *5"), 100); } #[test] fn test_divide() { assert_eq!(math("10/5"), 2); assert_eq!(math("500 / 2 "), 250); } #[test] fn test_equal() { assert_eq!(math("10 = 10"), 1); assert_eq!(math(" 2 =50"), 0); assert_eq!(math("0=0"), 1); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_add() {
- assert_eq!(math("1 + 2"), 3);
- assert_eq!(math("12+ 3456789"), 3456801);
- assert_eq!(math("50 + 10 0"), 150);
- }
- #[test]
- fn test_sub() {
- assert_eq!(math("1-3"), -2);
assert_eq!(math("500 - 150"), 350);- assert_eq!(math(" 500 - 150 "), 350);
- assert_eq!(math("0-2"), -2);
- }
- #[test]
- fn test_multiply() {
- assert_eq!(math("1* 30"), 30);
- assert_eq!(math("20 *5"), 100);
- }
- #[test]
- fn test_divide() {
- assert_eq!(math("10/5"), 2);
assert_eq!(math("500 / 2"), 250);- assert_eq!(math("500 / 2 "), 250);
- }
- #[test]
- fn test_equal() {
- assert_eq!(math("10 = 10"), 1);
assert_eq!(math("2 =50"), 0);- assert_eq!(math(" 2 =50"), 0);
- assert_eq!(math("0=0"), 1);
- }
- }