flâneur — a map of the web's best reading

Working with strings in Rust

fasterthanli.me · 13,894 words · saved by 1 readers

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. Let's start with a simple C program that prints its arguments. Okay! Simple enough. 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. Before proceeding, let's make sure we have a proper understanding of what's going on. Now we're using the %p format specifier, which prints.. pointers! Okay, so

Working with strings in Rust Working with strings in Rust Feb 19, 2020 45 min #rust Thanks to my sponsors: Steven Pham, budrick, Thehbadger, Xirvik Servers, Mathias Brossard, Olivier Peyrusse, Lucille Blumire, Antoine Rouaze, Ronen Cohen, Romain Ruetschi, Reto Trappitsch, Blake Johnson, Ross Williams, Yuriy Taraday, Tom Forbes, Brian L. Troutwine, Michal Hošna, Max von Forell, Timothée Gerber, Max Bruckner and 241 more Steven Pham, budrick, Thehbadger, Xirvik Servers, Mathias Brossard, Olivier Peyrusse, Lucille Blumire, Antoine Rouaze, Ronen Cohen, Romain Ruetschi, Reto Trappitsch, Blake Johns

Explore this link on the map →

saved by

related reading