Check symmetry in list

while learning rust, I stumbled on this function

#![feature(advanced_slice_patterns, slice_patterns)]

fn is_symmetric(list: &[u32]) -> bool {
    match list {
        [] | [_] => true,
        [x, inside.., y] if x == y => is_symmetric(inside),
        _ => false
    }
}

fn main() {
    let sym = &[0, 1, 4, 2, 4, 1, 0];
    assert!(is_symmetric(sym));

    let not_sym = &[0, 1, 7, 2, 4, 1, 0];
    assert!(!is_symmetric(not_sym));
}

in here.
But, when I tried to run it, the compiler gave an error that the feature is removed. So, I modified the function to use binding. Here's my function

fn is_symmetric(list: &[i32]) -> bool {
    match list {
        [] | [_] => true,
        inside @[a, .., b]  if a==b => is_symmetric(inside),
        _ => false,
    }
}

But, then also the compiler throws an error; says its not supported in stable linking to some github issue. So, is there any way to pattern match the list like the above, or to go with other method?

If that link is the resource you use for learning Rust, please note that it is super outdated. The current version of the book is found here.

Regarding your example, the syntax was changed here and became stable in this form:

fn is_symmetric(list: &[u32]) -> bool {
    match list {
        [] | [_] => true,
        [x, inside @ .., y] if x == y => is_symmetric(inside),
        _ => false
    }
}

fn main() {
    let sym = &[0, 1, 4, 2, 4, 1, 0];
    assert!(is_symmetric(sym));

    let not_sym = &[0, 1, 7, 2, 4, 1, 0];
    assert!(!is_symmetric(not_sym));
}
3 Likes

Thanks for the help