How to allocate array of T::default() values on TypedArena, without stack allocation first?

I can allocate a slice with rust's typed_arena like this:

use typed_arena::Arena;

let arena = Arena::new();
let abc = arena.alloc_extend("abcdefg".chars().take(3));

However, I want to create a simple array with T::zero() or T::default() values. In theory I could create a slice with all those values, but it would be on the stack, which is bad, and I'd have to copy them to the allocated space.

Is there a way to allocate an array and get the mutable slice directly, using some default value?

The only way I see is to create an iterator that always returns the default value for size times, but this does not seem like the best solution

I think this is the fastest solution. If the iterator implements TrustedLen, it should compile down to first reserving enough room for size items in the arena and then a loop that calls T::default() and immediately writes the value to the arena due to the SpecExtend implementation for TrustedLen iterators. The Repeat iterator returned by std::iter::repeat implements TrustedLen.

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.