You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust中Fn trait关联类型Output未指定错误,求替代解决方案

Alternative Ways to Implement a Function Accepting a Closure in Rust

You already fixed your initial error by using a where clause to constrain the generic type, but there are several other idiomatic approaches to achieve the same result in Rust. Let's walk through them:

1. Inline Generic Trait Constraint

Instead of using a where clause, you can apply the trait constraint directly to the generic parameter when declaring it. This is just a more compact version of your working solution:

fn from_func<T: Fn(i32, i32) -> i32>(a: i32, b: i32, func: T) -> i32 {
    func(a, b)
}

fn main() {
    let my_func = |a: i32, b: i32| a + b;
    println!("{:?}", from_func(15, 20, my_func));
}

This works exactly like your where clause implementation—both tell the compiler that T must implement the Fn trait with the specific signature (i32, i32) -> i32.

2. Use impl Trait for Parameter Type (Rust 1.26+)

If you don't need to reuse the generic type T elsewhere in the function (like in the return type), you can skip declaring a generic entirely and use impl Trait directly as the parameter type. This makes the code shorter and more readable:

fn from_func(a: i32, b: i32, func: impl Fn(i32, i32) -> i32) -> i32 {
    func(a, b)
}

fn main() {
    let my_func = |a: i32, b: i32| a + b;
    println!("{:?}", from_func(15, 20, my_func));
}

impl Fn(...) -> ... here means "any type that implements this specific Fn trait signature". It's a great choice for simple cases where you don't need to name the generic type.

3. Function Pointer Type (For Non-Capturing Closures/Functions)

If you only need to accept non-capturing closures (closures that don't reference variables from their surrounding environment) or regular named functions, you can use a function pointer type fn(i32, i32) -> i32 instead of a generic or trait bound:

// A regular named function
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn from_func(a: i32, b: i32, func: fn(i32, i32) -> i32) -> i32 {
    func(a, b)
}

fn main() {
    // Call with a named function
    println!("{:?}", from_func(15, 20, add));
    // Call with a non-capturing closure (it can be coerced to a function pointer)
    println!("{:?}", from_func(15, 20, |a, b| a + b));
}

Note that this won't work for closures that capture variables (e.g., let x = 10; let my_func = |a, b| a + b + x;), since those can't be converted to a function pointer.

Quick Recap of Your Initial Error

Your first attempt failed because writing func: Fn doesn't specify the trait's full signature. The Fn trait requires you to define both its input parameters and associated Output type (the return value). All the solutions above fix this by explicitly defining the full Fn signature the compiler expects.

内容的提问来源于stack exchange,提问作者Abhimanyu Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:35:13