#What is the difference between

14 messages · Page 1 of 1 (latest)

mossy axle
#

Option<&String> and &Option<String>

#

How is it managed under memory ?

#

Also whats the difference between &String and &str

solid sage
#

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>

#

extra tip: use .as_deref() to go &Option<String>Option<&str>

#

anyway

#

the other way around is not possible

solid sage
#

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