我应该为二维数组使用什么类型? [英] What type should I use for a 2-dimensional array?

查看:40
本文介绍了我应该为二维数组使用什么类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里a的类型有什么问题?

What is wrong with the type of a here?

fn foo(a: &[&[f64]], x: &[f64]) {
    for i in 0..3 {
        for j in 0..4 {
            println!("{}", a[i][j]);
        }
    }
}

fn main() {
    let A: [[f64; 4]; 3] = [
        [1.1, -0.2, 0.1, 1.6],
        [0.1, -1.2, -0.2, 2.3],
        [0.2, -0.1, 1.1, 1.5],
    ];
    let mut X: [f64; 3] = [0.0; 3];

    foo(&A, &X);
}

编译失败:

error[E0308]: mismatched types
  --> src/main.rs:17:9
   |
17 |     foo(&A, &X);
   |         ^^ expected slice, found array of 3 elements
   |
   = note: expected type `&[&[f64]]`
              found type `&[[f64; 4]; 3]`

推荐答案

数组切片是不同的类型.值得注意的是,数组具有固定大小,在编译时已知.切片具有固定大小,但仅在运行时知道.

Arrays are different types from slices. Notably, arrays have a fixed size, known at compile time. Slices have a fixed size, but known only at run time.

我在这里看到了两个直接的选择(参见 Levans 的另一个答案).第一个是将您的函数更改为仅接受对数组(或整个数组,如果您可以复制它或不介意放弃所有权)的引用:

I see two straight-forward choices here (see Levans answer for another). The first is to change your function to only accept references to arrays (or the whole array, if you can copy it or don't mind giving up ownership):

fn foo(a: &[[f64; 4]; 3], x: &[f64; 3]) {
    for i in 0..3 {
        for j in 0..4 {
            println!("{}", a[i][j]);
        }
    }
}

fn main() {
    let a = [
        [1.1, -0.2, 0.1, 1.6],
        [0.1, -1.2, -0.2, 2.3],
        [0.2, -0.1, 1.1, 1.5],
    ];

    let x = [0.0; 3];

    foo(&a, &x);
}

另一个简单的更改是将您的声明变成引用:

The other easy change is to make your declaration into references:

fn foo(a: &[&[f64]], x: &[f64]) {
    for i in 0..3 {
        for j in 0..4 {
            println!("{}", a[i][j]);
        }
    }
}

fn main() {
    let a = [
        &[1.1, -0.2, 0.1, 1.6][..],
        &[0.1, -1.2, -0.2, 2.3][..],
        &[0.2, -0.1, 1.1, 1.5][..],
    ];

    let x = [0.0; 3];

    foo(&a, &x);
}

请注意,在第二个示例中,当我们只传入 &a&x 时,我们可以使用对数组的引用的隐式强制转换为切片>.但是,对于a 中的嵌套数据,我们不能依赖它.a 已经被定义为数组的数组,我们不能改变元素类型.

Note that this second example, we can use the implicit coercion of a reference to an array to a slice, when we just pass in &a and &x. However, we cannot rely on that for the nested data in a. a has already been defined to be an array of arrays, and we can't change the element type.

还有一个警告 - 你真的应该在你的范围内使用切片的长度方法,否则你很容易恐慌!,如果你走到最后.

Also a word of caution - you really should use the length method of the slice in your ranges, otherwise you can easily panic! if you walk off the end.

fn foo(a: &[&[f64]], x: &[f64]) {
    for i in 0..a.len() {
        let z = &a[i];
        for j in 0..z.len() {
            println!("{}", z[j]);
        }
    }
}

我为满足 Rust 风格所做的其他风格更改:

Other stylistic changes I made to meet the Rust style:

  1. 变量是snake_case
  2. 后的空格:
  3. 后的空格;
  4. = 周围的空格
  5. 后的空格,
  1. variables are snake_case
  2. space after :
  3. space after ;
  4. space around =
  5. space after ,

这篇关于我应该为二维数组使用什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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