Chapter 1, Quick Introduction to Rust

Question 1

What is the difference between str and String?


A String is a reference stored in the stack that points to str, which is the data stored in the heap.

Question 2

Why can't string literals be passed through a function (string literal meaning str as opposed to &str)?


Because we do not know the size of a string literal at compile time. A String reference, on the other hand, is fixed, which is why it can be passed through to the function.

Question 3

How do we access the data belonging to a key in a hash map?


We use the get function, and then unwrap it since the get function merely returns an Option struct.

Question 4

When a function results in an error, can we handle ...

Get Rust Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.