Map on non-copy option in const context
⚓ Rust 📅 2025-12-22 👤 surdeus 👁️ 1I wanted to achieve the same result as Option::map in a const context. The function I wanted to map is the constructor of a tuple struct. I figured that I could simply "inline" the map call since the "function" I'm mapping is const and match is allowed in const contexts. However, I stumbled upon a strange error which do not really understand.
struct Wrapper<T>(T);
const fn map_wrapper<T>(option: Option<T>) -> Option<Wrapper<T>> {
match option {
Some(value) => Some(Wrapper(value)),
None => None,
}
}
Here is the error:
error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
--> src/lib.rs:3:25
|
3 | const fn map_wrapper<T>(option: Option<T>) -> Option<Wrapper<T>> {
| ^^^^^^ the destructor for this type cannot be evaluated in constant functions
...
8 | }
| - value is dropped here
The problem is that the (not necessarily const) destructor of option is apparently ran. This is what I don't understand since I thought that the match would move option causing drop not to be ran.
Why is option dropped? Is it not moved into the return value? Is it possible to accomplish my goals in stable Rust?
Thanks in advance.
2 posts - 2 participants
🏷️ Rust_feed