There's a question that always comes up when people pick up the Rust programming language: why are there two string types? Why is there String, and &a

Working with strings in Rust

submited by
Style Pass
2022-09-23 11:00:18

There's a question that always comes up when people pick up the Rust programming language: why are there two string types? Why is there String, and &str?

My Declarative Memory Management article answers the question partially, but there is a lot more to say about it, so let's run a few experiments and see if we can conjure up a thorough defense of Rust's approach over, say, C's.

We're using the standard C11 main function signature, which takes the number of argument (argc, for argument count) as an int, and an "array" of "strings" (argv, for argument vector) as a char**, or char *[].

Then we use the printf format specifier %s to print each argument as a string - followed by \n, a newline. And sure enough, it prints each argument on its own line.

Mhh. How does printf's %s specifier know when to stop printing? Since it just gets a single address, not a start and end address, or a start address and a length?

Leave a Comment