#What is the difference between
14 messages · Page 1 of 1 (latest)
How is it managed under memory ?
Also whats the difference between &String and &str
Memory-wise, the former is a nullable pointer that points to (non-null ptr, len, capacity)
while the latter is a non-null pointer that points to (nullable ptr, len capacity)
Basically, Option<&String> is what you want most of the time
Because &Option<String> can be trivially converted to Option<&String>
The Option type. See the module level documentation for more.
extra tip: use .as_deref() to go &Option<String> → Option<&str>
anyway
the other way around is not possible
&String goes through another level of indirection
it’s less useful, you basically never want it
&str is more flexible, since it can point to:
• part of a String rather than the entire thing
• A string literal instead
• a string allocated somewhere other than the heap