简体   繁体   English

如何定义可以返回给定 integer 类型的通用 function ?

[英]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:你有两个问题:

  1. 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<_>特征定义的任何转换
  2. You're mixing up two operations - coercion from a vector of characters to an integer, and your operation.您正在混合两个操作 - 从字符向量强制转换为 integer,以及您的操作。

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.

相关问题 如何从 Rust 中的成员 function 返回泛型类型 - How can I return a generic type from a member function in Rust 如何更改此功能的返回类型? - How can I change the return type of this function? 如何实现返回受整数类型约束的泛型的函数? - How can I implement a function that returns a generic constrained by an integral type? 如果返回类型是通用的,我可以返回 &str 输入的一部分吗? - Can I return a slice of a &str input if the return type is generic? 如何创建一个 is_prime function 对各种 integer 类型通用? - How can I create an is_prime function that is generic over various integer types? 如何创建返回泛型类型的修改实例的泛型 function? - How can I create a generic function that returns a modified instance of the generic type? 如何在Rust中定义泛型整数类型的无限范围? - How to define an infinite range of a generic integer type in Rust? 如果没有要返回的内容,如何从泛型函数返回有意义的东西? - How can I return something meaningful from a generic function if there is nothing to return? 如何定义返回其自己类型的Rust函数类型? - How can I define a Rust function type which returns its own type? 如何绑定结构的泛型类型,使其成为关联 function 的具体类型? - How can I bind a struct's generic type so that it is a concrete type for an associated function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM