What's mean `~const Drop` constaint in method?

Hi everyone

I get a puzzled when understand ~const constraint in method Option.map_or. the source code as follow

pub const fn map_or<U, F>(self, default: U, f: F) -> U
    where
        F: ~const FnOnce(T) -> U,
        F: ~const Drop,
        U: ~const Drop,
    {
        match self {
            Some(t) => f(t),
            None => default,
        }
    }

Can anyone give me some tip or suggestion about it ?

1 Like

You can ignore the “~const” part on the FnOnce bound, and you can ignore the “…: ~const Drop” bounds entirely (just how they aren’t appearing in the documentation either). This syntax is part of an in-development unstable language feature.

https://github.com/rust-lang/rust/issues/67792

On stable Rust, this method is essentially just

impl<T> Option<T> {
    pub fn map_or<U, F>(self, default: U, f: F) -> U
    where
        F: FnOnce(T) -> U,
    {
        match self {
            Some(t) => f(t),
            None => default,
        }
    }
}
3 Likes

thanks for your tips :rose:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.