Skip to content

Instantly share code, notes, and snippets.

@Fuwn
Created May 27, 2021 06:14
Show Gist options
  • Save Fuwn/8a4acbb75fbd7f6ceb2ef89281c2b354 to your computer and use it in GitHub Desktop.
Save Fuwn/8a4acbb75fbd7f6ceb2ef89281c2b354 to your computer and use it in GitHub Desktop.
Double dot operator for initializing vector with another vector's elements

I recently came across a situation where I had to push multiple vectors of thread handles to a central vector.

The first element that I was adding to the central thread comes as follows;

// This works.

// `example_crate::make()` returns a `std::thread::JoinHandle<()>`;
let mut threads = vec![example_crate::make()];

Now, I had another function which returns a vector of thread handles, if I wanted to push the elements of the return value to the threads variable, I would do;

// This works.

let mut threads = vec![example_crate::make()];

// `second_example_crate::make()` returns a `Vec<std::thread::JoinHandle<()>`;
threads.extend(second_example_crate::make());

My proposition is a struct like dot dot operator for expanding the elements of compatible vector types to the central vector (at initialization) in the following syntax;

// This currently does not work.

let mut threads = vec![example_crate::make(), ..second_example_crate::make()];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment