How to use slice patterns in a fixed array in Rust

2 Answers

0 votes
/*
In Rust, you can apply slice patterns directly to fixed-size arrays ([T; N]). 
Because the size of a fixed array is known at compile time, 
the Rust compiler enforces strict length checks, 
ensuring your patterns match the array's exact dimensions 
without requiring runtime length checks.
*/

/// Analyzes a fixed 4-element integer array using array and slice patterns.
pub fn analyze_quad(arr: [i32; 4]) -> String {
    match arr {
        // Pattern 1: Exact match for a specific sequence
        [1, 2, 3, 4] => String::from("Exact sequence: 1-2-3-4"),

        // Pattern 2: Match elements at specific positions, ignoring the middle two
        [first, _, _, last] if first == last => {
            format!("Bookend match: starts and ends with {}", first)
        }

        // Pattern 3: Match elements at the beginning, binding the remaining sub-slice
        // `rest` becomes a slice `&[i32]` of length 2.
        [0, rest @ ..] => {
            format!("Starts with zero, remaining elements: {:?}", rest)
        }

        // Pattern 4: Match with a middle rest pattern (e.g., first and last known)
        [first, .., last] => {
            format!("General quad: first={}, last={}", first, last)
        }
    }
}

fn main() {
    // Example 1: Exact sequence match
    let exact = [1, 2, 3, 4];
    println!("{}", analyze_quad(exact));

    // Example 2: Bookend match (first and last elements are equal)
    let bookend = [7, 9, 2, 7];
    println!("{}", analyze_quad(bookend));

    // Example 3: Leading element match with rest binding (`rest @ ..`)
    let zero_start = [0, 42, 99, 5];
    println!("{}", analyze_quad(zero_start));

    // Example 4: Catch-all via `[first, .., last]`
    let standard = [5, 10, 15, 20];
    println!("{}", analyze_quad(standard));
}


/*
run:

Exact sequence: 1-2-3-4
Bookend match: starts and ends with 7
Starts with zero, remaining elements: [42, 99, 5]
General quad: first=5, last=20

*/

 



answered 3 hours ago by avibootz
0 votes
/*
In Rust, you can apply slice patterns directly to fixed-size arrays ([T; N]). 
Because the size of a fixed array is known at compile time, 
the Rust compiler enforces strict length checks, 
ensuring your patterns match the array's exact dimensions 
without requiring runtime length checks.
*/

/// Evaluates a 3-element fixed-size integer array using slice patterns.
pub fn classify_array(arr: [i32; 3]) -> &'static str {
    match arr {
        // Pattern 1: Matches if the first element is 1. 
        // The underscore `_` acts as a wildcard, ignoring the second and third elements.
        [1, _, _] => "starts with one",

        // Pattern 2: Exhaustive catch-all for any other 3-element array.
        // Binds each element to variables `a`, `b`, and `c` and uses them.
        [a, b, c] => {
            // Using the bound variables resolves the unused variable warning
            println!("Elements were: {}, {}, {}", a, b, c);
            "starts with something else"
        }
    }
}

fn main() {
    let arr = [1, 2, 3];
    let result = classify_array(arr);
    println!("Result: {}\n", result); 

    let other_arr = [5, 2, 3];
    println!("Result: {}\n", classify_array(other_arr)); 

    let a = 10;
    let b = 20;
    let c = 30;
    let abc_arr = [a, b, c];
    println!("Result: {}", classify_array(abc_arr)); 
}


/*
run:

Result: starts with one

Elements were: 5, 2, 3
Result: starts with something else

Elements were: 10, 20, 30
Result: starts with something else

*/

 



answered 3 hours ago by avibootz
...