How to cast generic types that I know to be integers?
问题 I want to check return codes of C APIs in a generic way and the result must be free from C types such as libc::c_int . Are there any ways to write a function like fn check<S: PartialOrd + std::num::Zero, T> (x: S) -> Option<T> { if std::num::zero::<S>() <= x { Some(x as T) } else { None } } when I'm sure that S and T are integral types for all usages of check() ? The compiler rejects my code complaining error: non-scalar cast: `S` as `T` 回答1: Updated for Rust 1.x It is impossible to cast