Just remembered you can also do vec![value; length], and not just vec![value1, value2, value3...] to create a Vec.
-
Just remembered you can also do
vec![value; length]
, and not justvec![value1, value2, value3...]
to create aVec
. -
-
@steffo Also of note:
std::iter::repeat_n
, andIterator::take
andIterator::collect
... -
@WolvericCatkin oh yeah i knew about those, i had just completely forgotten about the
vec!
macro lol -
@steffo Tbh, reading the methods
vec!
ends up expanding out to, they seem like a bit of a mess...repeat_n
may be the better option from a technical standpoint... -
don't you lose information about capacity that way, though?
iirc iterators can only provide a
size_hint
, not give guarantees about their sizedoes the implementation of
collect<Vec<_>>
take them into account? -
@steffo
std::iter::repeat_n
has a guaranteed size, because it has a set number of iterators, and is cloning the item until the last iteration, so it's alsoExactLenIterator
.Depending on the provider,
Vec::from_iterator
can take advantage ofIterator::size_hint
. -
-
@WolvericCatkin oh that's very nice then!
i'll admit i haven't looked very much into the internals of
Iterator
and have been treating them pretty much as opaque types -
@steffo
vec!
ends up expanding out into a function that usesVec::extend_with
, which explicitly notes it has to compensate for compiler bugs, but otherwise has quite similar implementation... -
@steffo Bruh- ok, apparently looking at where
Vec::from_iterator
ends up, it does end up running into the same bug asvec!
eventually, just in a different method...Iterator handling with
Vec
is so frustratingly indirected...It does still pre-allocate with
Iterator::size_hint
though. -
@WolvericCatkin hahahaha, seems like there's no way around it