Getting a slice out of an ArrayView: cannot return value referencing function parameter

未关闭
#621 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
rust
领域
data

调研方向

Start with the minimal Rust example and the call at simulate/src/system_serialization.rs:58. Read the ArrayView, as_slice_memory_order, and aview1 definitions to understand the lifetime relationship, then reproduce the compiler error with the reported example. Done means establishing a supported way to construct the one-dimensional view, or documenting why the attempted conversion cannot compile.

由索引模型根据 Issue 内容生成。

描述

I am trying to write a function that interprets an ArrayView as row major order, but Rust doesn't like me extracting the slice underlying the ArrayView. Here is a mimimal example showing the problem:

fn as_standard_view<'a, T>(a: ArrayView<'a, T, Ix2>) -> ArrayView<'a, T, Ix1> {
    let slice = a.as_slice_memory_order().unwrap();
    aview1(slice)
}

This is the associated error:

error[E0515]: cannot return value referencing function parameter `a`
  --> simulate/src/system_serialization.rs:58:5
   |
57 |     let slice = a.as_slice_memory_order().unwrap();
   |                 - `a` is borrowed here
58 |     aview1(slice)
   |     ^^^^^^^^^^^^^ returns a value referencing data owned by the current function

I do understand the error, but I am not sure how to work around it. After all, the slice is held by the ArrayView, but not the data itself.

Any idea how to construct thay ArrayView without rust thinking that I am trying to reference something inside the function?

主要语言
Rust
星标
4.3k
派生
391
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-ndarray/ndarray 的其他 Issue

查看 rust-ndarray/ndarray 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。