You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rust.tex
+76Lines changed: 76 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2303,6 +2303,82 @@ \section{More About Cargo and Crates.io}
2303
2303
\end{frame}
2304
2304
2305
2305
\section{Smart Pointers}
2306
+
2307
+
2308
+
2309
+
\begin{frame}[fragile]
2310
+
\frametitle{Smart Pointers}
2311
+
\begin{itemize}
2312
+
\item A \textbf{pointer} is a general concept for a variable that contains an address in memory.
2313
+
\item The most common kind of pointer in Rust is a \textbf{reference}, which are indicated by the \mintinline{rust}|&| symbol and borrow the value they point to.
2314
+
\item\textbf{Smart pointers}, on the other hand, are \textbf{data structures} that act like a pointer but also have additional metadata and capabilities. The concept of smart pointers isn’t unique to Rust: smart pointers originated in C++ and exist in other languages as well.
2315
+
\item Rust has a \textbf{variety of smart pointers} defined in the standard library that provide functionality beyond that provided by references.
2316
+
\item Rust, with its concept of ownership and borrowing, has an additional difference between references and smart pointers: \textbf{while references only borrow data, in many cases, smart pointers own the data they point to}.
2317
+
\end{itemize}
2318
+
\end{frame}
2319
+
2320
+
2321
+
\begin{frame}[fragile]
2322
+
\frametitle{Smart Pointers (2)}
2323
+
\begin{itemize}
2324
+
\item\mintinline{rust}|String| and \mintinline{rust}|Vec<T>| are types count as smart pointers because they own some memory and allow you to manipulate it. They also have metadata and extra capabilities or guarantees.
2325
+
\item Smart pointers are usually implemented using structs. Unlike an ordinary struct, smart pointers implement the \mintinline{rust}|Deref| and \mintinline{rust}|Drop| traits. The \mintinline{rust}|Deref| trait allows an instance of the smart pointer struct to behave \textbf{like a reference} so you can write your code to work with either references or smart pointers. The \mintinline{rust}|Drop| trait allows you to customize the code that’s run when an instance of the smart pointer goes out of scope.
2326
+
\item the most common smart pointers in the standard library:
2327
+
\begin{itemize}
2328
+
\item\mintinline{rust}|Box<T>| for allocating values on the heap
2329
+
\item\mintinline{rust}|Rc<T>|, a reference counting type that enables multiple ownership
2330
+
\item\mintinline{rust}|Ref<T>| and \mintinline{rust}|RefMut<T>|, accessed through \mintinline{rust}|RefCell<T>|, a type that enforces the borrowing rules at runtime instead of compile time
2331
+
\end{itemize}
2332
+
\end{itemize}
2333
+
\end{frame}
2334
+
2335
+
2336
+
2337
+
2338
+
\begin{frame}[fragile]
2339
+
\frametitle{Using \mintinline{rust}|Box<T>| to Point to Data on the Heap}
2340
+
\begin{itemize}
2341
+
\item The most straightforward smart pointer is a box, whose type is written \mintinline{rust}|Box<T>|. Boxes allow you to \textbf{store data on the heap} rather than the stack. \textbf{What remains on the stack is the pointer to the heap data}.
2342
+
\item Boxes \textbf{don’t have performance overhead}, other than storing their data on the heap instead of on the stack. But they don’t have many extra capabilities either. You’ll use them most often in these situations:
2343
+
\begin{itemize}
2344
+
\item When you \textbf{have a type whose size can’t be known at compile time} and you \textbf{want to use a value of that type in a context that requires an exact size}
2345
+
\item When you \textbf{have a large amount of data and you want to transfer ownership but ensure the data won’t be copied} when you do so
2346
+
\item When you want to own a value and you care only that it’s a type that implements a particular trait rather than being of a specific type (known as a \textbf{trait object}, we learn later)
2347
+
\end{itemize}
2348
+
\end{itemize}
2349
+
2350
+
\end{frame}
2351
+
2352
+
2353
+
\begin{frame}[fragile]
2354
+
\frametitle{Using a \mintinline{rust}|Box<T>| to Store Data on the Heap}
2355
+
\inputminted{rust}{./code/smart1.rs}
2356
+
\end{frame}
2357
+
2358
+
2359
+
2360
+
\begin{frame}[fragile]
2361
+
\frametitle{Enabling Recursive Types with Boxes}
2362
+
A value of recursive type can have another value of the same type as part of itself. Recursive types pose an issue because at compile time Rust needs to know how much space a type takes up. However, the nesting of values of recursive types could theoretically continue infinitely, so Rust can’t know how much space the value needs. Because boxes have a known \textbf{size}, we can enable recursive types.
0 commit comments