Skip to content

Can't implement a const Option::unwrap because of "destructors cannot be evaluated at compile-time" error. #66753

Closed
@rodrimati1992

Description

It's not possible to implement Option::unwrap as a const fn like:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8ad0c1cdd371ca0488fab9d872bd6ffe

#![feature(const_fn)]
#![feature(const_if_match)]
#![feature(const_panic)]

const fn unwrap<T>(opt:Option<T>)->T{
    match opt {
        Some(x)=>x,
        None=>panic!("Trying to unwrap a None"),
    }
}

Because the function triggers this error:

error[E0493]: destructors cannot be evaluated at compile-time
 --> src/lib.rs:5:20
  |
5 | const fn unwrap<T>(opt:Option<T>)->T{
  |                    ^^^ constant functions cannot evaluate destructors

error: aborting due to previous error

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-destructorsArea: Destructors (`Drop`, …)B-unstableBlocker: Implemented in the nightly compiler and unstable.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions