Apply Discount to Prices LeetCode solution in Rust
1use std::fmt::Write as FmtWrite;23impl Solution {4pub fn discount_prices(sentence: String, discount: i32) -> String {5let mut new_str = String::new();67for (i, word) in sentence.split_whitespace().enumerate() {8if i != 0 {9new_str.push(' ');10}11if word.len() > 0 {12if word.as_bytes()[0] == b'$' {13if (&word[1..]).chars().all(|c| c >= '0' && c <= '9') {14if let Ok(num) = &word[1..].parse::<f64>() {15let n = num * ((100 - discount) as f64 / 100_f64);1617write!(&mut new_str, "${:.2}", n);18continue;19}20}21}22}2324new_str.push_str(word);25}2627new_str28}29}