【问题标题】:Rust ndarray arithmetic operation unexpected type mismatchRust ndarray 算术运算意外类型不匹配
【发布时间】:2019-12-03 08:03:08
【问题描述】:

我在尝试对 ndarray crate 中的两个 Array1s 执行算术运算时遇到问题。

我已尝试将我的问题简化为以下表述:

#[macro_use(array)]
extern crate ndarray;

use ndarray::Array1;

fn main() {
  let a: Array1<i8> = array![1, 2, 3];
  let baz = &a - array![1, 2, 3];
  println!("{:#?}", baz);
}

它失败了:

  |
8 |   let baz = &a - array![1, 2, 3];
  |                ^ expected struct `ndarray::ArrayBase`, found i8
  |

根据documentation,我应该可以减去两个Array1s,array! 创建一个Array1

我做错了什么?

【问题讨论】:

    标签: arrays rust arithmetic-expressions


    【解决方案1】:

    我应该更仔细地阅读documentation

    &A @ &A which produces a new Array
    B @ A which consumes B, updates it with the result, and returns it
    B @ &A which consumes B, updates it with the result, and returns it
    C @= &A which performs an arithmetic operation in place
    

    由于某种原因,没有&amp;A @ B 的情况。第二个参数不能被消费。要么没有,要么只有第一个。由于我不想在这里消耗a,所以我需要引用array! 宏的返回值。

    所以解决办法是:

    let baz = &a - &array![1, 2, 3];
    

    编译器错误在这里并没有真正的帮助......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2018-01-21
      • 1970-01-01
      相关资源
      最近更新 更多