What is the idiomatic way to select integer types in APIs written in Rust?
For example, in C, it is often used inteven for function arguments, which are expected to be positive. For simplicity, we use a type specifier inteven for arguments that can be set to one byte.
Rust has a very strict type system, so all type conversions must be explicit. For example, by creating Vec<T>::len()return usize, the developers of the Rust standard library pledged to embed tedious conversions in isizenon-trivial calculations (where negative values may appear).
So should I use specialized integer types when writing the API or should I just stick with it isize?
Update:
He will ask more specifically. I am designing a type called NeuralNetwith the following instance constructor:
pub struct NeuralNet<F: traits::Float, A: Activator<F>> {
phantom: marker::PhantomData<A>,
inputs: isize,
input_neurons: isize,
folding_step: isize,
layers: isize,
coefs: vec::Vec<F>
}
impl<F: traits::Float, A: Activator<F>> NeuralNet<F, A> {
pub fn new(inputs: isize, input_neurons: isize,
folding_step: isize, layers: isize,
coefs: &[F]) -> NeuralNet<F, A> {
...
}
Here inputsand input_neuronscan be set u16, layersyou can set in u8and folding_stepin i8. Should I use this type or just stick isizein order not to compromise the constructor?
ababo source
share