MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/n7cbsv/the_xy_problem/gxsgb22/
r/programming • u/azhenley • May 07 '21
68 comments sorted by
View all comments
Show parent comment
4
It's not defined for arrays. In general, though, it does depend on the type.
When I have to do this, I generally do one of two things. If the expected number of items is small I do:
let mut iter = todo!(); let data = [ iter.next(), iter.next(), iter.next(), iter.next(), ]; assert!(iter.next().is_none());
If it's a fixed number of items but longer than four or five, I'll do:
let mut iter = todo!(); let mut data = [0; 64]; iter.zip(data.iter_mut()).for_each(|i, d| *d = i);
Depending on the needs, I might unwrap inside of the first one or use MaybeUninit in the second, but the general pattern is the same.
unwrap
MaybeUninit
0 u/backtickbot May 11 '21 Fixed formatting.Hello, Elnof: code blocks using triple backticks (```) don't work on all versions of Reddit!Some users see this / this instead.To fix this, indent every line with 4 spaces instead.FAQYou can opt out by replying with backtickopt6 to this comment.
0
Fixed formatting.
Hello, Elnof: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.
View all comments
Show parent comment
4
u/Elnof May 11 '21
It's not defined for arrays. In general, though, it does depend on the type.
When I have to do this, I generally do one of two things. If the expected number of items is small I do:
If it's a fixed number of items but longer than four or five, I'll do:
Depending on the needs, I might
unwrapinside of the first one or useMaybeUninitin the second, but the general pattern is the same.