Crate captur

source ·
Expand description


Starting in Rust 2021, Rust will no longer capture whole structs and instead will only capture a disjoint set of the fields used in a closure. In some cases, it is necessary to capture the structs to retain a particular drop order. This macro will capture the struct within the closure, ensuring the correct drop order.


use captur::capture;
struct SomeStruct {
	a: String,
	b: String,

impl SomeStruct {
	fn new() -> Self {
		Self {
			a: String::from("a"),
			b: String::from("b"),

let some_struct = SomeStruct::new();
let result = || {
	format!("{}", some_struct.b)

println!("{}", result());


  • Create a reference to a struct, that will ensure it is captured by a closure.