Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active January 23, 2020 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lupyuen/fc9321c2338e6fdda8827aff7236c9fb to your computer and use it in GitHub Desktop.
Save lupyuen/fc9321c2338e6fdda8827aff7236c9fb to your computer and use it in GitHub Desktop.

Why are the strings mashed together?

In the PineTime Debugger when inspecting Rust variables, we'll see strings mashed together like...

I AM PINETIMEcalled...

This highlights an important difference between Rust Strings and C Strings. C Strings are stored with a null as the last character. (We call this the Terminating Null)

Rust Strings are stored as two components: String Length and String Contents. The String Length is stored in a field named length. (We can see this in the debugger)

The String Contents for all Rust Strings are mashed together into the same memory space, without the Terminating Null... Because we already have a String Length that tells us how to extract our String Contents from the mashup.

When passing Rust Strings to C Functions (like Mynewt Functions), it's important to convert the strings correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment