Monday, January 15, 2024

Rust pop-quiz: infinite lists

#Rust pop-quiz

Code an infinite list. Scope the behavior, then implement.

A sample implementation could be that it repeats the first or last element when iterated forever, e.g.:

[1,2,3,4,4,4,4,4,...]

"I love you this muchoooooooooo"...

[["Hello"],["world"],["world"],...]

BONUS!

Implement fmt::Debug for the infinite list (OT) so that

mk_inf(&[].to_vec(), 1) -> "[1, 1, 1, ...]"
mk_inf(&[1,2,3].to_vec(), 4) -> "[1, 2, 3, 4, 4, 4, ...]"

Make sure the compiler does not issue warnings! (so: BONUS-BONUS!)

No comments:

Post a Comment