-
-
Save akolybelnikov/86782af232f6fba950be4b825c458501 to your computer and use it in GitHub Desktop.
Exercism Rust track: Sublist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, PartialEq)] | |
pub enum Comparison { | |
Equal, | |
Sublist, | |
Superlist, | |
Unequal, | |
} | |
fn convert_to_strings<T: PartialEq + std::fmt::Display>(_list: &[T]) -> String { | |
let str_vec: Vec<String> = _list | |
.iter() | |
.map(|n| n.to_string()) // map every integer to a string | |
.collect(); | |
str_vec.join("") // join the strings | |
} | |
pub fn sublist<T: PartialEq + std::fmt::Display>( | |
_first_list: &[T], | |
_second_list: &[T], | |
) -> Comparison { | |
// Convert numbers to strings | |
let pair = ( | |
convert_to_strings(_first_list), | |
convert_to_strings(_second_list), | |
); | |
// Use match with guards to filter branches | |
match pair { | |
(a, b) if a == b => Comparison::Equal, | |
(a, b) if a.contains(&b) => Comparison::Superlist, | |
(a, b) if b.contains(&a) => Comparison::Sublist, | |
_ => Comparison::Unequal, | |
} | |
} | |
fn main() { | |
let a = vec![1u8, 2, 3, 4, 5, 6]; | |
let b = vec![]; | |
print!("{:?}", sublist(&a, &b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment