Rust Function Overloading - Call for Experimentation

Rust Function Overloading - Call for Experimentation

Rust 函数重载——实验征集

Aug. 19, 2026 · teor on behalf of The Language Team 2026年8月19日 · teor 代表语言团队

In partnership with the Rust Foundation’s Rust-C++ Interop Initiative, the Rust Project has been experimenting with function overloading for FFI bindings. This experiment is now at a stage where compiler and interop tool developers can start exploring function overloading. 通过与 Rust 基金会的 Rust-C++ 互操作计划合作,Rust 项目一直在尝试为 FFI(外部函数接口)绑定引入函数重载。目前该实验已进入阶段,编译器和互操作工具开发者可以开始探索函数重载功能。

Stable Rust already supports a form of overloading using tuples and traits, but calling these overloaded functions looks strange, because the overloaded arguments have to be passed as a single tuple argument, like this: hypot((2.0, 3.0, 6.0)). Stable Rust also allows overloading of built-in operators with user-defined types, via traits like Add (the + operator) and Neg (the - value negation operator). 目前的稳定版 Rust 已经通过元组(tuple)和特征(trait)支持了一种重载形式,但调用这些重载函数看起来很奇怪,因为重载参数必须作为单个元组参数传递,例如:hypot((2.0, 3.0, 6.0))。稳定版 Rust 还允许通过 Add+ 运算符)和 Neg- 取反运算符)等特征,对内置运算符进行用户自定义类型的重载。

We are running an unstable nightly Rust language experiment to answer questions like: How much overloading can we do with Rust’s existing trait system? Could this help us call C++ from Rust ergonomically? In the tradition of yeet (to avoid bikeshedding), we are using basic syntax in the first stage of the experiment: the #[rustc_splat] attribute. Alternative syntaxes can be considered later, if the experiment generates useful outcomes. 我们正在运行一项不稳定的 Nightly 版 Rust 语言实验,旨在回答以下问题:利用 Rust 现有的特征系统,我们能实现多大程度的重载?这能否帮助我们更符合人体工程学地从 Rust 调用 C++?秉承“yeet”的传统(为了避免在琐碎细节上争论),我们在实验的第一阶段使用了基础语法:#[rustc_splat] 属性。如果实验产生有用的结果,后续可以考虑其他替代语法。

Experimental Function Overloading

实验性函数重载

Rust nightly builds from 2026-07-31 onwards have experimental support for more ergonomic function and method overloading, using the incomplete “splat” compiler feature; if you’re a compiler or interop tool developer, we encourage you to experiment with it! This experiment lets overloaded functions be called with separate arguments, like this: hypot(2.0, 3.0, 6.0). No double parentheses required! But type inference and type checking still happen as they would in a stable Rust overload. 从 2026 年 7 月 31 日起的 Rust Nightly 构建版本,通过不完整的“splat”编译器特性,对更符合人体工程学的函数和方法重载提供了实验性支持;如果你是编译器或互操作工具开发者,我们鼓励你尝试一下!该实验允许以独立参数调用重载函数,例如:hypot(2.0, 3.0, 6.0)。无需双重括号!但类型推断和类型检查仍会像在稳定版 Rust 重载中那样进行。

We are experimenting with splat to get a feel for the complexity of the implementation, and to see if it solves some language interoperability use cases. Like most Rust language experiments, this nightly feature has no RFC, and can change or be removed at any time. 我们正在尝试使用 splat 来评估实现的复杂性,并观察它是否能解决某些语言互操作的使用场景。像大多数 Rust 语言实验一样,此 Nightly 特性没有 RFC,且随时可能更改或移除。

Experiment Design

实验设计

We expect the feature to change significantly in future, or to be replaced by a more ergonomic interface. In this spirit, Ajay Singh, a Rust Project Outreachy intern, is working on a macro to make splat-based overloading more ergonomic. You can find his work in the rust-foundation/overloading-macros repository on GitHub. 我们预计该特性在未来会有重大变化,或者被更符合人体工程学的接口所取代。本着这种精神,Rust 项目 Outreachy 实习生 Ajay Singh 正在开发一个宏,旨在使基于 splat 的重载更加易用。你可以在 GitHub 的 rust-foundation/overloading-macros 仓库中找到他的工作。

The design axioms for this feature are:

  • “keep Rust nice”
  • make calling overloaded FFI functions easy
  • preserve foreign language maintainability
  • select the overload most developers would expect 该特性的设计准则包括:
  • “保持 Rust 的优雅”
  • 使调用重载的 FFI 函数变得简单
  • 保持外部语言的可维护性
  • 选择大多数开发者所预期的重载方式

This could be a challenging design, because different programming languages have different overload resolution rules. 这可能是一个具有挑战性的设计,因为不同的编程语言具有不同的重载解析规则。

Example Usage

使用示例

Rust overloading aims to support a range of programming languages. This example uses C++ because it is well known, and has significant existing interop tooling. Here is some Rust code that calls the overloaded C++ hypot (hypotenuse) function, using “splat” to create corresponding overloads in Rust. Rust 重载旨在支持多种编程语言。本示例使用 C++,因为它广为人知且拥有大量现有的互操作工具。以下是一些调用重载 C++ hypot(斜边)函数的 Rust 代码,它使用“splat”在 Rust 中创建了相应的重载。

#![feature(splat, tuple_trait)]
#![expect(incomplete_features)]
use cpp::cpp;
use std::{ffi::c_double, marker::Tuple};

cpp! {{ #include <cmath> }}

/// The arguments of an overloaded C++ `hypot` function.
trait HypotArgs: Tuple {
    type Output;
    fn call_hypot(self) -> Self::Output;
}

/// Calls the overloaded C++ `std::hypot` function with the given arguments.
fn hypot<Args: HypotArgs>(#[rustc_splat] args: Args) -> <Args as HypotArgs>::Output {
    args.call_hypot()
}

/// A 2-argument `hypot` overload.
impl HypotArgs for (c_double, c_double) {
    type Output = c_double;
    fn call_hypot(self) -> c_double {
        let (x, y) = self;
        unsafe { cpp!([x as "double", y as "double"] -> c_double as "double" {
            return std::hypot(x, y);
        }) }
    }
}

/// A 3-argument `hypot` overload.
impl HypotArgs for (c_double, c_double, c_double) {
    type Output = c_double;
    fn call_hypot(self) -> c_double {
        let (x, y, z) = self;
        unsafe { cpp!([x as "double", y as "double", z as "double"] -> c_double as "double" {
            return std::hypot(x, y, z);
        }) }
    }
}

fn main() {
    println!("|(3, 4)| = {}", hypot(3.0, 4.0));
    println!("|(2, 3, 6)| = {}", hypot(2.0, 3.0, 6.0));
}

This example uses the cpp crate to inline C++ code in a Rust file. A full runnable example is available on GitHub. You can also run a minimal Rust-only example online, in the Rust playground. Nadrieril’s original “Overloading at Home” code from his recent write-up of “splat”, can also be run in the Rust playground. 此示例使用 cpp crate 在 Rust 文件中内联 C++ 代码。完整的可运行示例可在 GitHub 上找到。你也可以在 Rust Playground 在线运行一个最小化的纯 Rust 示例。Nadrieril 在其近期关于“splat”的文章中提到的原始“Overloading at Home”代码,也可以在 Rust Playground 中运行。

Limitations

局限性

As stated above, splat is currently an incomplete compiler feature, and is only available in the nightly Rust compiler. Splat is also unergonomic, and we want to change that as part of the next design phase. 如上所述,splat 目前是一个不完整的编译器特性,仅在 Nightly 版 Rust 编译器中可用。Splat 目前也不够符合人体工程学,我们希望在下一阶段的设计中对此进行改进。

Support for splatted function arguments in rustdoc just merged on 12 August. Splatted arguments display as an ellipsis (…), rather than the argument name. This syntax is unstable, currently only used for display in rustdoc, and can change at any time. 对 rustdoc 中 splat 函数参数的支持已于 8 月 12 日合并。Splat 参数显示为省略号(…),而不是参数名称。此语法是不稳定的,目前仅用于 rustdoc 中的显示,且随时可能更改。

For example: fn example(#[rustc_splat] args: (u32, String)); Is displayed as: fn example(…: (u32, String)); 例如: fn example(#[rustc_splat] args: (u32, String)); 显示为: fn example(…: (u32, String));

We’ve also recently merged splat support for function pointers, if you’re seeing an internal compiler error, please update to the latest nightly. If you want overloading support for function pointers, please let us know about your use case in the #t-lang/interop channel on Zulip. And there is an ongoing Rust standard library experiment using splat for variable-argument smallest and greatest functions. Hopefully they will be available soon on nightly. 我们最近还合并了对函数指针的 splat 支持,如果你遇到内部编译器错误,请更新到最新的 Nightly 版本。如果你需要函数指针的重载支持,请在 Zulip 的 #t-lang/interop 频道告诉我们你的使用场景。此外,Rust 标准库正在进行一项实验,使用 splat 实现变参的最小值和最大值函数。希望它们很快能在 Nightly 版本中提供。

Experimenters will find other bugs – some have already been found in the last few months – and some overloading functionality is out of scope for now. If you think you’ve found a bug or limitation, ask us about it in the #t-lang/interop channel on Zulip. 实验者会发现其他 Bug——过去几个月已经发现了一些——并且目前某些重载功能不在范围内。如果你认为自己发现了 Bug 或局限性,请在 Zulip 的 #t-lang/interop 频道向我们咨询。

Credits

致谢

This work would not be possible without Google’s generous funding and support of the Rust Foundation’s Rust-C++ Interop Initiative. It is driven by the “Nightly support for function overloading in FFI bindings” Rust Project goal, and it is an outcome of the “C++/Rust Interop Problem Space Mapping” Rust Project goal. 如果没有 Google 对 Rust 基金会“Rust-C++ 互操作计划”的慷慨资助和支持,这项工作是不可能完成的。它由 Rust 项目目标“为 FFI 绑定中的函数重载提供 Nightly 支持”所驱动,也是“C++/Rust 互操作问题空间映射”Rust 项目目标的成果。