什么特征界限可用于匹配 Rust 中的所有无符号整数类型?

Say*_*osh 2 integer traits rust

我想为 rust 中所有无符号整数类型的通用函数定义特征绑定。函数签名如下:

fn process_unsigned<T: UnsignedInt>(param: T) -> T {
    if param meets condition {
        // process and return result as T
    }
    else {
        return 0; // has to meet the trait bound on T
    }
}
Run Code Online (Sandbox Code Playgroud)

T 的合适界限是多少?

Jon*_*der 5

Rustnum为 Rust 的数字类型提供了具有抽象特征的包。您可以尝试使用 的num::Unsigned特征T