小编wea*_*ary的帖子

在 Rust 中,将泛型与整数进行比较需要什么特征

如何将计算结果与泛型进行比较?T 总是一些无符号整数类型(u64、u32 等),所以代码片段中的应该可以工作,但是我如何说服 rust 编译器?

fn reproduction<T>(val: T) -> bool
where
    T: PartialOrd
{
    let var_of_type_integer = 7; // actually the result of a calculation
    if val < var_of_type_integer { // ERROR: expected type parameter, found integer
        return true;
    }
    false
}
Run Code Online (Sandbox Code Playgroud)

generics traits rust

1
推荐指数
1
解决办法
381
查看次数

标签 统计

generics ×1

rust ×1

traits ×1