[英]How can I define a generic function that can return a given integer type?
I'd like to define a function that can return a number whose type is specified when the function is called.我想定义一个 function ,它可以返回一个在调用 function 时指定类型的数字。 The function takes a buffer ( Vec<u8>
) and returns numeric value, eg function 采用缓冲区( Vec<u8>
)并返回数值,例如
let byte = buf_to_num<u8>(&buf);
let integer = buf_to_num<u32>(&buf);
The buffer contains an ASCII string that represents a number, eg b"827"
, where each byte is the ASCII code of a digit.缓冲区包含一个代表数字的 ASCII 字符串,例如b"827"
,其中每个字节是一个数字的 ASCII 码。
This is my non-working code:这是我的非工作代码:
extern crate num;
use num::Integer;
use std::ops::{MulAssign, AddAssign};
fn buf_to_num<T: Integer + MulAssign + AddAssign>(buf: &Vec::<u8>) -> T {
let mut result : T;
for byte in buf {
result *= 10;
result += (byte - b'0');
}
result
}
I get mismatched type errors for both the addition and the multiplication lines ( expected type T, found u32
).对于加法和乘法行( expected type T, found u32
),我得到不匹配的类型错误。 So I guess my problem is how to tell the type system that T
can be expressed in terms of a literal 10
or in terms of the result of (byte - b'0')
?所以我想我的问题是如何告诉类型系统T
可以用文字10
或(byte - b'0')
的结果来表示?
Welcome to the joys of having to specify every single operation you're using as a generic.欢迎享受将您使用的每一个操作都指定为泛型的乐趣。 It's a pain, but it is worth.这是一种痛苦,但它是值得的。
You have two problems:你有两个问题:
result *= 10;
without a corresponding From<_>
definition.没有相应的From<_>
定义。 This is because, when you specify "10", there is no way for the compiler to know what "10" as a T
means - it knows primitive types, and any conversion you defined by implementing From<_>
traits这是因为,当您指定“10”时,编译器无法知道“10”作为T
的含义 - 它知道原始类型以及您通过实现From<_>
特征定义的任何转换We need to make two assumptions for this:为此,我们需要做两个假设:
We will require From<u32>
so we can cap our numbers to u32
我们将需要From<u32>
以便我们可以将我们的数字限制为u32
We will also clarify your logic and convert each u8
to char
so we can use to_digit()
to convert that to u32
, before making use of From<u32>
to get a T
.我们还将阐明您的逻辑并将每个u8
转换为char
以便我们可以使用to_digit()
将其转换为u32
,然后再使用From<u32>
获取T
。
use std::ops::{MulAssign, AddAssign}; fn parse_to_i<T: From<u32> + MulAssign + AddAssign>(buf: &[u8]) -> T { let mut buffer:T = (0 as u32).into(); for o in buf { buffer *= 10.into(); buffer += (*o as char).to_digit(10).unwrap_or(0).into(); } buffer }
You can convince yourself of its behavior on the playground你可以让自己相信它在操场上的行为
The multiplication is resolved by force-casting the constant as u8
, which makes it benefit from our requirement of From<u8>
for T and allows the rust compiler to know we're not doing silly stuff.乘法是通过将常量强制转换为u8
来解决的,这使其受益于我们对From<u8>
对 T 的要求,并允许 rust 编译器知道我们没有做傻事。
The final change is to set result
to have a default value of 0.最后的更改是将result
设置为默认值 0。
Let me know if this makes sense to you (or if it doesn't), and I'll be glad to elaborate further if there is a problem:-)让我知道这对您是否有意义(或者如果没有),如果有问题,我会很高兴进一步阐述:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.