Skip to content

Best Practice: ArrayView vs Pointer #1059

Closed
@JP-Ellis

Description

@JP-Ellis

I was wondering whether there was a best practive when writing functions that expect arrays, specifically when they are not taking ownership of the array. I couldn't see this being addressed in the documentation or in an existing issue so here's a few things I think could be worth thinking about:

  1. Should functions prefer to take ArrayView over the equivalent Array, and what about using a pointer vs by value? For example, which of the following would be 'best'?

    fn sum(a: ArrayView1<f64>) -> f64;
    fn sum(a: &ArrayView1<f64>) -> f64;
    fn sum(a: &Array1<f64>) -> f64;
  2. When muteable data is expected, is there a preference to using ArrayViewMut vs &mut Array?

    fn increment(mut a: ArrayViewMut1<f64>) -> f64;
    fn increment(a: &mut Array1<f64>) -> f64;

I would expect that for most applications, the ArrayView and ArrayViewMut are preferred as they are more general.

Is the fact that they are views into an array mean that they behave more like pointers and thus &ArrayView is redundant?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions