From 584c1ed5f7374d8f7292d47d53012e87f8d5d3c4 Mon Sep 17 00:00:00 2001 From: Pandicon <70060103+Pandicon@users.noreply.github.com> Date: Sat, 8 Oct 2022 14:51:03 +0200 Subject: [PATCH 1/2] feat(Rust): Add max number finder --- codes/Rust/max_number.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 codes/Rust/max_number.rs diff --git a/codes/Rust/max_number.rs b/codes/Rust/max_number.rs new file mode 100644 index 0000000..ba36919 --- /dev/null +++ b/codes/Rust/max_number.rs @@ -0,0 +1,19 @@ +use std::io::{self, Write}; + +fn main() { + let mut numbers = vec![]; + while numbers.is_empty() { + print!("Enter your numbers separated with spaces: "); + io::stdout().flush().expect("Failed to flush console."); + let line = input(); + numbers = line.trim().split(' ').filter_map(|x| x.parse::().ok()).collect::>(); + } + let max_number = numbers.iter().max().expect("Failed to get the maximum number."); + println!("Maximum number from {:?} is {}", numbers, max_number); +} + +fn input() -> String { + let mut line = String::new(); + io::stdin().read_line(&mut line).expect("Failed to read input line"); + line +} \ No newline at end of file From c9aa3db9f841247f790f3860b5149ddeb6c8b1a0 Mon Sep 17 00:00:00 2001 From: Pandicon <70060103+Pandicon@users.noreply.github.com> Date: Sat, 8 Oct 2022 15:01:09 +0200 Subject: [PATCH 2/2] feat(Rust): Add a palindrome checker --- codes/Rust/is_palindrome.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 codes/Rust/is_palindrome.rs diff --git a/codes/Rust/is_palindrome.rs b/codes/Rust/is_palindrome.rs new file mode 100644 index 0000000..da3f759 --- /dev/null +++ b/codes/Rust/is_palindrome.rs @@ -0,0 +1,30 @@ +use std::io::{self, Write}; + +fn main() { + print!("Input your string: "); + std::io::stdout().flush().expect("Failed to flush the console."); + let line = input(); + let mut string = line.trim().to_string(); + print!("Do you want the check to be case sensitive (Y for yes, anything else for no): "); + std::io::stdout().flush().expect("Failed to flush the console."); + let line = input(); + let case_sensitive = line.trim().to_lowercase() == "y"; + if !case_sensitive { + string = string.to_lowercase(); + } + let characters = string.chars().collect::>(); + let characters_length = characters.len(); + for i in 0..(characters_length + 1) / 2 { + if characters[i] != characters[characters_length - i - 1] { + println!("It is NOT a palindrome"); + return; + } + } + println!("It is a palindrome"); +} + +fn input() -> String { + let mut line = String::new(); + io::stdin().read_line(&mut line).expect("Failed to read input line"); + line +} \ No newline at end of file