Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(PartialEq, Debug)]
- pub enum Comparison {
- Equal,
- Unequal,
- Sublist,
- Superlist,
- }
- fn sublists<'a, T>(l: &'a [T]) -> impl 'a + Iterator {
- let empty: &[T] = &[]; // I can't put this directly in once as it gets seen as &[_; 0]
- std::iter::once(empty).chain((0..l.len()).flat_map(move |i| l.windows(i)))
- }
- pub fn sublist<T>(l1: &[T], l2: &[T]) -> Comparison
- where
- T: PartialEq,
- {
- if l1 == l2 {
- Comparison::Equal
- } else if sublists(l2).find(l1).is_some() {
- Comparison::Sublist
- } else if sublists(l1).find(l2).is_some() {
- Comparison::Superlist
- } else {
- Comparison::Unequal
- }
- }
- fn main() {
- assert_eq!(Comparison::Sublist, sublist(&[1, 2, 3], &[1, 2, 3, 4, 5]));
- }
Add Comment
Please, Sign In to add comment