4

All the answers to this question about passing an array from C to Rust use std::slice::from_raw_parts to convert the raw C pointer and some length information into a Rust. In an embedded context (MOS 6502 in my case), there might not be a std library available. So in a #![no_std] context, what is the nicest way of passing an array from C to Rust for (potentially mutable) processing?

Elias Holzmann
  • 3,216
  • 2
  • 17
  • 33
Cactus
  • 27,075
  • 9
  • 69
  • 149
  • 1
    I added the [rust-no-std] tag to your question and needed to remove the [pointers] tag as the question would have exceeded the [five tags limit](https://meta.stackexchange.com/q/34732/288173) after my edit. If you feel that the [pointers] tag is more relevant to the question than [rust-no-std], feel free to revert (but note that I also fixed a typo – you probably don't want to revert that part of the edit either way). – Elias Holzmann Sep 02 '21 at 13:05
  • @EliasHolzmann thanks, the [rust-no-std] tag is exactly what I should have used, I just didn't know about it. – Cactus Sep 02 '21 at 13:06

1 Answers1

6

While std::slice::from_raw_parts is not available in a no_std environment, core::slice::from_raw_parts is available, so you can simply use that.

In general, anything that is in std and not somehow platform dependent (so, not I/O, heap allocation and the like) should also be available in no_std environments through the core crate.

Elias Holzmann
  • 3,216
  • 2
  • 17
  • 33
  • 2
    Additionally, since Rust 2018 you can also use `core` in non-`no_std` crates. `std::slice::from_raw_parts` is just an alias to `core::slice::from_raw_parts`, which can be used directly **all the time**. – Tim Diekmann Sep 02 '21 at 20:31