如何用一个通用的固定长度数组定义一个函数? [英] How to define a function with a generic fixed-length array?

查看:138
本文介绍了如何用一个通用的固定长度数组定义一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一个泛型类型(比如 f32 f64 ),但您可能还需要一个通用长度,但不是动态长度。 b

下面是一个带有2个参数的平方函数的例子。

 使用std :: ops :: {Add,AddAssign,Sub,Mul}; 

const CAN_THIS_BE_GENERIC:usize = 2;

fn squared_length< T>(
a:& [T; CAN_THIS_BE_GENERIC],
b:& [T; CAN_THIS_BE_GENERIC]
) - > T
其中T:
复制+
添加+
AddAssign +
添加<输出= T> +
Sub< Output = T> +
Mul<输出= T>
{
let mut d:T = a [0] - a [0]; (零)(
for(elem_a,elem_b)in a.iter()。zip(b.iter()){
let elem_dim:T = * elem_a - * elem_b;
d + = elem_dim * elem_dim;
}
return d;
}

fn main(){
println!(Length A!{} ,squared_length ::< f64>(& [0.5,3.5],& [10.0,0.0]));
println!(Length B!{},squared_length ::< i32>( & [ - 3.0,0.0]); [b] [b] [b] ,& [9.0,0.0]));
}

目前矢量长度是设置为2



是否可以定义泛型函数,其中大小不是动态的,而是泛型的,类型的泛型可以是相同的?

解决方案

不是,目前的Rust 1.14是不可能的,这个(通常称为类型级整数请求功能,但它尚未在Rust中提供。



已经有一些RF关于这一点,最近的一次令人惊讶并且非常有希望: RFC:const依赖型系统。但即使它被接受,它也需要多个Rust版本才能最终登陆(例如,它很可能不会在2016年进入稳定编译器)。



有几个模板可以模拟类型级别的整数,例如 type-num 。这是有用的,但我不会说它是一个完整的选择。



请注意:有时使用类型级别整数并不是必须的。你的例子也适用于动态尺寸。甚至更好:因为你的函数很小,所以可能会被内联,然后优化器可能在编译时计算出所有大小。因此,如果性能是使用类型级别整数的唯一原因,则可能不需要。


Take an operation on arrays, squaring the length for example.

It's useful to have a generic type (such as f32, f64), but you may also want a generic length, but not a dynamic length.

Here is an example of a squared function that takes 2 arguments.

use std::ops::{Add, AddAssign, Sub, Mul};

const CAN_THIS_BE_GENERIC: usize = 2;

fn squared_length<T>(
    a: &[T; CAN_THIS_BE_GENERIC],
    b: &[T; CAN_THIS_BE_GENERIC]
) -> T
    where T:
        Copy +
        Add +
        AddAssign +
        Add<Output=T> +
        Sub<Output=T> +
        Mul<Output=T>
{
    let mut d: T = a[0] - a[0];  // zero :(
    for (elem_a, elem_b) in a.iter().zip(b.iter()) {
        let elem_dim: T = *elem_a - *elem_b;
        d += elem_dim * elem_dim;
    }
    return d;
}

fn main() {
    println!("Length A! {}", squared_length::<f64>(&[0.5, 3.5], &[10.0, 0.0]));
    println!("Length B! {}", squared_length::<i32>(&[10, -6], &[4, 8]));
    println!("Length C! {}", squared_length::<f32>(&[-3.0, 0.0], &[9.0, 0.0]));
}

Currently the vector length is set at 2.

Is it possible to define generic functions where the size is not dynamic, but generic, the same way types can be generic?

解决方案

No, it's not possible in the current Rust 1.14. This (often called "type level integers") is a long requested feature, but it's not yet available in Rust.

There have been some RFCs on this; the most recent one came as a surprise and is pretty promising: RFC: const-dependent type system. But even if it's accepted it will take multiple Rust versions to finally land (e.g. it's very unlikely to make it into the stable compiler in 2016 still).

There are a few crates simulating type level integers, like type-num. It's kind of usable, but I wouldn't call it a full alternative.

Please also note: sometimes it's not really necessary to use type level integer. Your example would work with dynamic sizes, too. Better even: because your function is so small, it's likely to be inlined and then the optimizer can probably figure out all sizes at compile time. So if performance was the only reason to use type level integers, it might not be necessary.

这篇关于如何用一个通用的固定长度数组定义一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆