How to check whether an array is subset of another array in Rust

1 Answer

0 votes
fn is_subset(arr1: &[i32], arr2: &[i32]) -> bool {
    for &item in arr2 {
        if !arr1.contains(&item) {
            return false;
        }
    }
    true
}

fn main() {
    let arr1 = [5, 1, 8, 12, 40, 7, 9, 100];
    let arr2 = [8, 40, 9, 1];

    if is_subset(&arr1, &arr2) {
        println!("yes");
    } else {
        println!("no");
    }
}



/*
run:

yes

*/

 



answered Oct 4, 2025 by avibootz
...