# Pass \[\[f64\]\] reference to function

**URL:** <https://users.rust-lang.org/t/pass-f64-reference-to-function/33109>\
**Category:** help\
**Created:** [September 30, 2019, 3:56pm UTC](https://users.rust-lang.org/t/pass-f64-reference-to-function/33109 "2019-09-30T15:56:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthomedal](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anthomedal](https://users.rust-lang.org/u/anthomedal)\
**Post date:** [September 30, 2019, 3:56pm UTC](https://users.rust-lang.org/t/pass-f64-reference-to-function/33109/1 "2019-09-30T15:56:06Z")

</div>

Hello everyone,

Is it possible to passe a [[f64]] by reference to a function ? For example :

```
const N: usize = 100;

let a: [[f64;N];N] = [[0.0;N];N];

my_function(&a)

```

I know this code will not compile, but it exists a way to do the same efficiently ?

I have the same question to a [[[f64]]] array.

Just another question : is it true that vector has less fast as array in Rust ?

Thank you !

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [September 30, 2019, 4:17pm UTC](https://users.rust-lang.org/t/pass-f64-reference-to-function/33109/2 "2019-09-30T16:17:40Z")

</div>

No you cannot

> [@anthomedal](#):
>
> Just another question : is it true that vector has less fast as array in Rust ?

Yes, vectors are slower than arrays, but you will likely never see them in your profiler, so I wouldn't worry about it.

Also Rust doesn't support arrays thar well yet, so it will be much easier to use vectors.

---

<div class="post-metadata">

**Author:** ![anthomedal](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anthomedal](https://users.rust-lang.org/u/anthomedal)\
**Post date:** [September 30, 2019, 4:25pm UTC](https://users.rust-lang.org/t/pass-f64-reference-to-function/33109/3 "2019-09-30T16:25:26Z")

</div>

Okay, thank you !

---

<div class="post-metadata">

**Author:** ![vmedea](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/vmedea/32/15128_2.png) [@vmedea](https://users.rust-lang.org/u/vmedea)\
**Post date:** [September 30, 2019, 5:18pm UTC](https://users.rust-lang.org/t/pass-f64-reference-to-function/33109/4 "2019-09-30T17:18:22Z")

</div>

right, for

```rust
fn my_function(a: &[[f64;N];N]) {
}

```

it does need to know N in advance, there's currently no way to parameterize the function type over different array sizes, this would need [const generics](https://github.com/rust-lang/rust/issues/44580)

`&[[f64;N];N]` doesn't automatically coerce to a slice of slices `[[f64]]` because it has a very different representation, if you have

```rust
fn my_function(a: &[&[f64]]) {
}

```

then to call it, you'd need to generate an vector (or array) of slices first

```rust
fn main() {
    let a: [[f64;N];N] = [[0.0;N];N];
    let rows = a.iter().map(|x| &x[..]).collect::<Vec<_>>();
    my_function(&rows);
}

```

> [@anthomedal](#):
>
> Just another question : is it true that vector has less fast as array in Rust ?

not with regard to access, as far as i know—both an array and a vector lay out the elements consecutively in memory—the difference in performance is that a Vector requires a separate heap allocation to create, whereas arrays are allocated on the stack (or as part of the structure they're part of)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [December 29, 2019, 5:18pm UTC](https://users.rust-lang.org/t/pass-f64-reference-to-function/33109/5 "2019-12-29T17:18:27Z")

</div>

Closed on December 29, 2019, 5:18pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
