Rust tip that drop the variable without Send trait before next await

#rust #rustTips

Today, I want to shuffle a vec in an async function. The code is as follows:

async fn send_data() {
	let mut shuffled_blocks: Vec<_> = blocks_map.into_iter().collect();  
	let mut rng = rand::thread_rng();  
	shuffled_blocks.shuffle(&mut rng);
}

But I got a error by rust compiler.

Pasted image 20240705154653.png

From above the tip, the solution is to drop it before the await happen, like this:

async fn send_data() {
	let mut shuffled_blocks: Vec<_> = blocks_map.into_iter().collect();  
	let mut shuffled_blocks: Vec<_> = {  
		let mut rng = rand::thread_rng();  
		shuffled_blocks.shuffle(&mut rng);  
		shuffled_blocks  
	};
}