Rust 中的空泛型参数用于 Option

huangapple go评论110阅读模式
英文:

Rust empty explicit generic argument for Option<impl Fn(i64)>

问题

The code you provided is in Rust, and it seems like you're encountering type inference issues when trying to call the apply(None) function. Here's a possible solution:

fn main() {
    let a = apply(Some(|i| i + 1));

    let b = apply(None::<fn(i64) -> i64>);

    println!("a: {}; b: {}", a, b);
}

fn apply(f: Option<impl Fn(i64) -> i64>) -> i64 {
    let i = 0_i64;
    match f {
        Some(f) => f(i),
        None => i,
    }
}

In the revised code, I specified the generic type parameter for None as None::<fn(i64) -> i64>, which should help the Rust compiler infer the type correctly.

英文:

I have Option&lt;impl Fn(i64) -&gt; i64&gt; as an argument to a function apply() and I want to pass None value. apply(None). How do I write the apply(None) function call correctly so it compiles?

fn main(){
	let a = apply(Some(|i| i + 1));
	
	let b = apply(None);
	let b2 = apply(None::&lt;dyn Fn(i64) -&gt; i64&gt;);
	
	println!(&quot;a: {0}; b: {1}&quot;, a, b)
}

fn apply(f: Option&lt;impl Fn(i64) -&gt; i64&gt;) -&gt; i64 {
	let i = 0_i64;
	match f {
		Some(f) =&gt; f(i),
		None =&gt; i,
	}
}

compile error:

Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
 --&gt; src/main.rs:4:16
  |
4 |     let b = apply(None);
  |                   ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
  |
help: consider specifying the generic argument
  |
4 |     let b = apply(None::&lt;T&gt;);
  |                       +++++

error[E0283]: type annotations needed
  --&gt; src/main.rs:4:16
   |
4  |     let b = apply(None);
   |             ----- ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
   |             |
   |             required by a bound introduced by this call
   |
   = note: multiple `impl`s satisfying `_: Fn&lt;(i64,)&gt;` found in the following crates: `alloc`, `core`:
           - impl&lt;A, F&gt; Fn&lt;A&gt; for &amp;F
             where A: Tuple, F: Fn&lt;A&gt;, F: ?Sized;
           - impl&lt;Args, F, A&gt; Fn&lt;Args&gt; for Box&lt;F, A&gt;
             where Args: Tuple, F: Fn&lt;Args&gt;, A: Allocator, F: ?Sized;
note: required by a bound in `apply`
  --&gt; src/main.rs:10:25
   |
10 | fn apply(f: Option&lt;impl Fn(i64) -&gt; i64&gt;) -&gt; i64 {
   |                         ^^^^^^^^^^^^^^ required by this bound in `apply`
help: consider specifying the generic argument
   |
4  |     let b = apply(None::&lt;T&gt;);
   |                       +++++

Some errors have detailed explanations: E0282, E0283.
For more information about an error, try `rustc --explain E0282`.

I tried let b2 = apply(None::&lt;dyn Fn(i64) -&gt; i64&gt;); but it still doesn't compile the errors are

 Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `dyn Fn(i64) -&gt; i64` cannot be known at compilation time
 --&gt; src/main.rs:5:20
  |
5 |     let b2 = apply(None::&lt;dyn Fn(i64) -&gt; i64&gt;);
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn&#39;t have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `dyn Fn(i64) -&gt; i64`
note: required by a bound in `None`
 --&gt; /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/option.rs:567:5

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin &quot;playground&quot;) due to previous error

答案1

得分: 3

The type dyn Fn(i64) -> i64 is unsized, meaning you can't store it in an enum like Option.

But a trait object reference is sized, so you can use &dyn instead:

apply(None::<&dyn Fn(i64) -> i64>);

However, it's probably better to use a function pointer type instead:

apply(None::<fn(i64) -> i64>);

playground

英文:

The type dyn Fn(i64) -&gt; i64 is unsized, meaning you can't store it in an enum like Option.

But a trait object reference is sized, so you can use &amp;dyn instead:

apply(None::&lt;&amp;dyn Fn(i64) -&gt; i64&gt;);

However, it's probably better to use a function pointer type instead:

apply(None::&lt;fn(i64) -&gt; i64&gt;);

playground

huangapple
  • 本文由 发表于 2023年8月4日 22:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定