Skip to content

Instantly share code, notes, and snippets.

@koopa1338
Created April 8, 2023 01:27
Show Gist options
  • Save koopa1338/8d9476c653a0488090a1be33be25ae07 to your computer and use it in GitHub Desktop.
Save koopa1338/8d9476c653a0488090a1be33be25ae07 to your computer and use it in GitHub Desktop.
Rust string types
String:
buffer
/ capacity
/ / length
/ / /
+–––+–––+–––+
stack frame │ • │ 8 │ 6 │ <- my_string: String
+–│–+–––+–––+
[–│–––––––– capacity –––––––––––]
+–V–+–––+–––+–––+–––+–––+–––+–––+
heap │ F │ e │ r │ r │ i │ s │ │ │
+–––+–––+–––+–––+–––+–––+–––+–––+
[––––––– length ––––––––]
&str:
my_str: String sub_str: &str
[–––––––––––-–] [–––––––]
+–––+––––+––––+–––+–––+–––+
stack frame │ • │ 16 │ 13 │ │ • │ 6 │
+–│–+––––+––––+–––+–│–+–––+
│ │
│ +–+
│ │
│ │
│ [–│––––– str –––––––]
+–V–+–––+–––+–––+–––+–V–+–––+–-–+–––+–––+
heap │ R │ u │ s │ t │ │ r │ o │ c │ k │ s │
+–––+–––+–––+–––+–––+–––+–––+–––+–––+–––+
string literals:
my_str: &str
[–––––––]
+–––+–––+
stack frame │ • │ 4 │
+–│–+–––+
+––+
preallocated +–V–+–––+–––+–––+
read-only │ R │ u │ s │ t │
memory +–––+–––+–––+–––+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment