5

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?

2
  • 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 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). Commented Sep 2, 2021 at 13:05
  • @EliasHolzmann thanks, the [rust-no-std] tag is exactly what I should have used, I just didn't know about it. Commented Sep 2, 2021 at 13:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.