Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created February 18, 2022 22:52
Show Gist options
  • Save rust-play/0dd2d649c107ce0f2119373e6868fadf to your computer and use it in GitHub Desktop.
Save rust-play/0dd2d649c107ce0f2119373e6868fadf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[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