Why Not Just Use Fn, FnMut, or FnOnce as Types?

Closures in Rust already fall into one of three well-defined categories (Fn, FnMut, and FnOnce) based on their capture behavior.

If there are only these three possible types, why bother with impl? Why not directly use them as types instead of impl Trait?

5

If there are only these three possible types

This is not correct. Fn, FnMut, and FnOnce are not types, they are traits. In fact, they cannot be just types.

While any ordinary fn you define can be represented as a simple pointer, closures are a completely different thing. They can capture their environment, which in practice means that they can use the variables defined near them. This also means that they need to store these variables somewhere, as you can’t really know when exactly the closure will be executed. Every closure might want to capture different types of variables, all having different sizes, so there can’t be a one-size-fits-all type that could contain any sort of “context” that you might possibly use. Hence, through the compiler magic, an unnamable type is created, which you can confirm with a simple code snippet:

let mut x = 5;
let f = || {
    x += 1;
};

println!("size_of({}) = {}", std::any::type_name_of_val(&f), std::mem::size_of_val(&f));

This prints:

size_of(playground::main::{{closure}}) = 8

Under the hood, what happens in the above code is something close to this:

let mut x = 5;
struct Closure<'a> {
    // closure context
    x: &'a mut i32,
}
impl FnOnce<()> for Closure<'_> {
    type Output = ();
    extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
        // closure body
        *self.x += 1;
    }
}
let f = Closure { x: &mut x };

// now it can be called:
f();

The closure object in this case must contain a mutable reference to the variable it modifies, so the closure type is 8 bytes long (on a 64-bit platform). You might want to experiment a little with different closures capturing different things and notice that the closure type size directly depends on the variables it captures.


See also: How do Rust closures work and how does it execute a closure?

That is because each individual function or closure has a separate and unique type.

Fn, FnMut and FnOnce are traits, not types.

Take the following two examples:

fn add(l: i32, r: i32) -> i32 {
    l + r
}
fn subtract(l: i32, r: i32) -> i32 {
    l - r
}

These two functions have the exact same signature, therefore they implement the exact same function traits.

However, the type of these functions is not the same. Each individual function is given its own unique, unnamed type.

This is why you have to use them through those traits.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why Not Just Use Fn, FnMut, or FnOnce as Types?

Closures in Rust already fall into one of three well-defined categories (Fn, FnMut, and FnOnce) based on their capture behavior.

If there are only these three possible types, why bother with impl? Why not directly use them as types instead of impl Trait?

5

If there are only these three possible types

This is not correct. Fn, FnMut, and FnOnce are not types, they are traits. In fact, they cannot be just types.

While any ordinary fn you define can be represented as a simple pointer, closures are a completely different thing. They can capture their environment, which in practice means that they can use the variables defined near them. This also means that they need to store these variables somewhere, as you can’t really know when exactly the closure will be executed. Every closure might want to capture different types of variables, all having different sizes, so there can’t be a one-size-fits-all type that could contain any sort of “context” that you might possibly use. Hence, through the compiler magic, an unnamable type is created, which you can confirm with a simple code snippet:

let mut x = 5;
let f = || {
    x += 1;
};

println!("size_of({}) = {}", std::any::type_name_of_val(&f), std::mem::size_of_val(&f));

This prints:

size_of(playground::main::{{closure}}) = 8

Under the hood, what happens in the above code is something close to this:

let mut x = 5;
struct Closure<'a> {
    // closure context
    x: &'a mut i32,
}
impl FnOnce<()> for Closure<'_> {
    type Output = ();
    extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
        // closure body
        *self.x += 1;
    }
}
let f = Closure { x: &mut x };

// now it can be called:
f();

The closure object in this case must contain a mutable reference to the variable it modifies, so the closure type is 8 bytes long (on a 64-bit platform). You might want to experiment a little with different closures capturing different things and notice that the closure type size directly depends on the variables it captures.


See also: How do Rust closures work and how does it execute a closure?

That is because each individual function or closure has a separate and unique type.

Fn, FnMut and FnOnce are traits, not types.

Take the following two examples:

fn add(l: i32, r: i32) -> i32 {
    l + r
}
fn subtract(l: i32, r: i32) -> i32 {
    l - r
}

These two functions have the exact same signature, therefore they implement the exact same function traits.

However, the type of these functions is not the same. Each individual function is given its own unique, unnamed type.

This is why you have to use them through those traits.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật