Open tdz opened 7 years ago
In MicroPython we have the traditional pythonic read method which always allocates a new string, and then we also support read_into, where the caller provides a pointer and length and then no allocations are required. For rust we could provde a mutable slice (which is effectively a pointer and length).
Here's my take on the read_into
option: https://play.rust-lang.org/?gist=8bb5c89db3491cc01eb303804ef7b3f4&version=stable&backtrace=0
Your for loop should be 0.. rather than 1..
And something else isn't quite right. If you fill buf with 0x45 ('E') then I don't get the expected results. https://gist.github.com/584a6c649c07200175d2c4f3793c4f4a
no, because the [u8] length doesn't know about the terminating 0
ha you're right 0 .. N loops to 0 to N-1. I though it was inclusive. The other error is more interesting!
Hm, indeed it's not doing the right thing... see https://play.rust-lang.org/?gist=0091cfe93c016259b158a4a4fa6306eb&version=stable&backtrace=0
Thanks for the comments. Sorry, I'm still learning how to do this in Rust.
I guess you did it this way to not create strings on the heap but instead reuse the same buffer? That is clearly not idiomatic Rust, let me think how we could achieve the same goals with less unsafe calls.