Side effects of constructing an explicitly unused object

Say I have a struct that provides a method that does some work (has useful side effects) and then returns an instance of itself. Do I have to use that instance somehow in order to guarantee that the code having the side effects will execute?

As per C++ standard, RVO and NRVO can elide the “as-if” rule. However, I’d expect different behavior from Rust, especially when using the _ = ... idiom. Now I’m wondering if I’m wrong in doing so.

To give a concrete example, let’s look at the esp_hal::gpio::Io::new() which calls Self::new_with_priority() and before returning an instance of itself sets up the io-driver as a side effect. Can I then use the function for its side effects, but explicitly discard the returned object, like so:

let _ = esp_hal::gpio::Io::new();

In contrast to C++, I’d expect the compiler to not elide the side effects. Furthermore, I would expect it to elide the construction of an Ioinstance. In fact, that’s exactly how I’d interpret the semantics/intent behind the _ = ... idiom. Should I update my beliefs?

8

It’s important to note that you can destructure with let. More generally, you can pattern-match with let as long as the pattern is irrefutable.

_ is the pattern that means “match anything at all and don’t bind it.” In other words, anything that matches _ in a pattern will be dropped.

So we can infer from this that let _ = anything(); is almost1 semantically identical in every way to just anything();. Using let _, the value returned from anything() is dropped because of the behavior described above. Not using let _, the value returned from anything() is dropped at the end of the statement because it is an unused temporary value.

All that to say, it doesn’t much matter whether you say let _ = function(); or just function();. In terms of what happens to the function’s return value, they do the same thing at the same point in time.

Additionally, Rust doesn’t have a special notion of constructors like C++ does. Functions that create values can be called “constructor functions” but this is just terminology; at the language level, there is no conceptual difference between a function that creates a Self or any other function.

Rust also doesn’t have a concept of pure functions or those with side effects. If a function is actually pure and the result is unused, the optimizer might be able to determine that the whole thing is a giant no-op, but if the function does have side effects, such as mutating state or doing I/O, the optimizer will be unable to elide the function call in its entirety.

The summary is that the compiler is not going to optimize away that function call, assuming it does have observable side effects2. This is true regardless of whether or not you use the let _ syntax.


1 The “almost” is that let _ = y; counts as a usage of y, while y on its own does not. This is commonly used to ignore Results returned from a function when one does not care if the operation failed. let _ = fallible_operation(); will suppress the “unused Result that must be used” warning.

2 As a curiosity, note that Rust does not consider allocation to be a side effect, and therefore allocations can be optimized away in some cases.

4

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