Skip to content

Instantly share code, notes, and snippets.

@byte-sourcerer
Created October 14, 2025 08:09
Show Gist options
  • Select an option

  • Save byte-sourcerer/72b65854b744139a3f6c3eee37276fdf to your computer and use it in GitHub Desktop.

Select an option

Save byte-sourcerer/72b65854b744139a3f6c3eee37276fdf to your computer and use it in GitHub Desktop.
Reuse Rust's `Vec`
// https://davidlattimore.github.io/posts/2025/09/02/rustforge-wild-performance-tricks.html
// https://github.com/rust-lang/libs-team/issues/674
fn reuse_vec<T, U>(mut v: Vec<T>) -> Vec<U> {
const {
assert!(size_of::<T>() == size_of::<U>());
assert!(align_of::<T>() == align_of::<U>());
}
v.clear();
v.into_iter().map(|_| unreachable!()).collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment