# \[solved\] How to pass None to a function when an Option\<Closure\> is expected?

**URL:** <https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956>\
**Category:** help\
**Created:** [May 23, 2017, 9:25am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956 "2017-05-23T09:25:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukas.pustina](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/lukas.pustina/32/2134_2.png) [@lukas.pustina](https://users.rust-lang.org/u/lukas.pustina)\
**Post date:** [May 23, 2017, 9:25am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/1 "2017-05-23T09:25:58Z")

</div>

Hello everybody,

I'm fighting with a function that takes an `Option<FnMut(String) -> ()>` as parameter when I want to pass `None`, because the type of `T` cannot be deferred.

```rust
fn maybe_closure<T: FnMut(String) -> ()>(closure: Option<T>) -> () {
...
}

// Works
maybe_closure(Some(...));

// Does not compile
maybe_closure(None);

```

The compiler tells me

```rust
11 | maybe_closure(None);
    | ^^^^^^^^^^^^^^^^^^ cannot infer type for `T`
    |
    = note: type annotations or generic parameter binding required

```

I get that. But I have no idea how to actually specify `T`, because if I use `None::<FnMut(String) -> ()` the compiler explains

```rust
    |
11 | maybe_closure("Deutsche Bank", None::<FnMut(String) -> ()>);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `std::ops::FnMut(std::string::String)`
    |
    = note: `std::ops::FnMut(std::string::String)` does not have a constant size known at compile-time
    = note: required by `std::prelude::v1::None`

```

I also get that.

But I have no idea how to solve this issue satisfying the compiler and me.

Any help is appreciated.  
Cheers Lukas

---

<div class="post-metadata">

**Author:** ![garro](https://avatars.discourse-cdn.com/v4/letter/g/f4b2a3/32.png) [@garro](https://users.rust-lang.org/u/garro)\
**Post date:** [May 23, 2017, 9:41am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/2 "2017-05-23T09:41:07Z")

</div>

I think `None::<Box<FnMut(String) -> ()>>` should work. The error is because the size of the closure is unknown at compile time, so you have to box it.  
Someone correct me if I'm wrong 'cause I'm not so expert.

---

<div class="post-metadata">

**Author:** ![xfix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/xfix/32/4122_2.png) [@xfix](https://users.rust-lang.org/u/xfix)\
**Post date:** [May 23, 2017, 9:46am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/3 "2017-05-23T09:46:29Z")

</div>

That's because Rust doesn't know what is the type of `T`. An easy workaround would be to provide any type that fits `T`, the easiest pick would be function literal pointer.

```
maybe_closure(None::<fn(_)>);

```

Unfortunately, this is ugly, you may want to try providing a different API, depending on what `None` actually means in your API.

---

<div class="post-metadata">

**Author:** ![lukas.pustina](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/lukas.pustina/32/2134_2.png) [@lukas.pustina](https://users.rust-lang.org/u/lukas.pustina)\
**Post date:** [May 23, 2017, 9:46am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/4 "2017-05-23T09:46:49Z")

</div>

Thanks @garro, but that does not adhere to the function's signature, because now an `Option<Box<T>>` is passed instead of `Option<T>`.

I'd rather not change the signature of the function, but convince the compiler 🙂

---

<div class="post-metadata">

**Author:** ![lukas.pustina](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/lukas.pustina/32/2134_2.png) [@lukas.pustina](https://users.rust-lang.org/u/lukas.pustina)\
**Post date:** [May 23, 2017, 9:51am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/5 "2017-05-23T09:51:38Z")

</div>

Thanks @xfix, that does the trick and it's not the worst I've seen 🙂

The semantics is that the real function contains a long running algorithm and may or may not report progress with a callback passed as a closure.

---

<div class="post-metadata">

**Author:** ![xfix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/xfix/32/4122_2.png) [@xfix](https://users.rust-lang.org/u/xfix)\
**Post date:** [May 23, 2017, 9:53am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/6 "2017-05-23T09:53:32Z")

</div>

If semantics of `None` are essentially doing nothing, then don't accept `Option`, and just specify empty function like `|| {}` as an argument. As the function is generic over types, and closures have different types the compiler will be able to optimize that out by inlining. No need to micro-optimise

---

<div class="post-metadata">

**Author:** ![lukas.pustina](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/lukas.pustina/32/2134_2.png) [@lukas.pustina](https://users.rust-lang.org/u/lukas.pustina)\
**Post date:** [May 23, 2017, 9:56am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/7 "2017-05-23T09:56:50Z")

</div>

I actually prefer `Option` because it better communicates intent, but I'll keep your idea in mind.

---

<div class="post-metadata">

**Author:** ![Dr\_Emann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dr_emann/32/876_2.png) [@Dr\_Emann](https://users.rust-lang.org/u/Dr_Emann)\
**Post date:** [May 23, 2017, 2:02pm UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/8 "2017-05-23T14:02:18Z")

</div>

`T = Box<FnMut(String) -> ()>` satisfies `T: FnMut(String) -> ()`. It wouldn't require changing the code. But `fn(String)` is better

---

<div class="post-metadata">

**Author:** ![luser](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/luser/32/14268_2.png) [@luser](https://users.rust-lang.org/u/luser)\
**Post date:** [May 23, 2017, 7:19pm UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/9 "2017-05-23T19:19:05Z")

</div>

I hit this exact same issue while adding a new test in my code. I had a helper function and wanted to add an optional function to invoke at a certain point. After struggling with the same errors you did I gave up and just defined a [`fn nothing(...)`](https://github.com/mozilla/sccache/blob/48f1196f490b3f01a63fe2fba2f40144b51aaba2/src/compiler/rust.rs#L1097) to use instead of `None`.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:44am UTC](https://users.rust-lang.org/t/solved-how-to-pass-none-to-a-function-when-an-option-closure-is-expected/10956/10 "2023-01-12T08:44:05Z")

</div>

Closed on January 12, 2023, 8:44am UTC


