Rust

Rust

Made by DeepSource

Found manual implementation of vec.first() RS-W1116

Anti-pattern
Minor

The Vec type provides the first() and first_mut() methods to retrieve the first element of the vector. Similarly, the VecDeque type provides the front() and front_mut() methods.

Prefer using these instead of get(0) and get_mut(0).

Bad practice

fn foo() {
    let v = vec![1, 2, 3, 4];
    let v_first = v.get(0);
    let dq = VecDeque::from([1, 2, 3, 4]);
    let dq_first_mut = dq.get_mut(0);
}

Recommended

fn foo() {
    let v = vec![1, 2, 3, 4];
    let v_first = v.first();
    let dq = VecDeque::from([1, 2, 3, 4]);
    let dq_first_mut = dq.front_mut();
}