Created
October 14, 2025 08:09
-
-
Save byte-sourcerer/72b65854b744139a3f6c3eee37276fdf to your computer and use it in GitHub Desktop.
Reuse Rust's `Vec`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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