How to get wraparound in slice for out of range bounds?

I want like this behaviour

fn main() {
    let t = &[1, 2, 3][..5];
    assert_eq!(t, &[1, 2, 3, 1, 2])
    
}

Is there is a method that do like this?

Not quite like that, what you can do is

let items: Vec<_> = [1,2,3].iter().cycle().take(5).collect();

println!("{:?}", items);

This produces the output:

[1, 2, 3, 1, 2]
3 Likes

good enough for me

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.