mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:14:32 -06:00
Standardize heading formatting
- Bump subsections in a level - Title case - Code formatting
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Generics
|
||||
## Generics Syntax
|
||||
|
||||
We've already hinted at generics previously, but never dug into what exactly
|
||||
they are. You can always recognize when generics are used by the way that they
|
||||
@@ -13,7 +13,7 @@ In the same way that a function like `foo(x: i32)` can be called with a
|
||||
specific value such as `foo(5)`, a `Vec<T>` can be created with a specific
|
||||
type, like `Vec<i32>`.
|
||||
|
||||
## Generic data types
|
||||
### Generic Data Types
|
||||
|
||||
Let's dive into generic data types in a bit more detail. We previously learned
|
||||
about the `Option<T>` type, but we never examined its definition. Let's try to
|
||||
@@ -204,7 +204,7 @@ You can have as many type parameters as you'd like. Similarly to parameters of
|
||||
values, if you have a lot of them, it can get quite confusing, so try to keep
|
||||
the number of them small if you can.
|
||||
|
||||
## Generic functions and methods
|
||||
### Generic Functions and Methods
|
||||
|
||||
In a similar way to data structures, we can use the `<>` syntax to write
|
||||
functions:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Traits
|
||||
## Traits
|
||||
|
||||
At the end of the last section, we had this code:
|
||||
|
||||
@@ -139,7 +139,7 @@ Why do we need the trait in scope? Imagine we had two traits with the same
|
||||
method definition, and our `Point` struct implemented both. We wouldn't know
|
||||
which method we were trying to call. `use` makes it explicit.
|
||||
|
||||
## Trait bounds
|
||||
### Trait Bounds
|
||||
|
||||
We previously knew how to define methods, so what makes traits special? Well,
|
||||
imagine we had a function that wanted to call `print` for any type that supports
|
||||
@@ -220,7 +220,7 @@ fn print<T: Display>(argument: T) {
|
||||
|
||||
Now that we've said "for any type that implements `Display`," this works well.
|
||||
|
||||
## Where syntax
|
||||
### `where` Syntax
|
||||
|
||||
When bounds start getting complicated, there is another syntax that's a bit
|
||||
cleaner: `where`. And in fact, our original error referred to it. It looks
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Lifetime syntax
|
||||
## Lifetime Syntax
|
||||
|
||||
So far, we've talked about two ways in which Rust allows you to abstract over
|
||||
different things: generic type parameters let us abstract over types, and
|
||||
@@ -14,7 +14,7 @@ to do is talk about the very basics of lifetimes, so that when you see the
|
||||
syntax in documentation or other places, you won't be totally lost. Chapter XX
|
||||
will contain more advanced information about everything lifetimes can do.
|
||||
|
||||
## Core syntax
|
||||
### Core Syntax
|
||||
|
||||
We've talked about references previously, but we left something important out.
|
||||
As it turns out, every reference in Rust has a lifetime. Lifetimes have a
|
||||
@@ -47,7 +47,7 @@ functions that take generic types, we parameterize references with lifetimes.
|
||||
|
||||
So, that's the syntax, but _why_? What does a lifetime do, anyway?
|
||||
|
||||
## No dangling references!
|
||||
### No Dangling References!
|
||||
|
||||
Consider this program:
|
||||
|
||||
@@ -145,7 +145,7 @@ do this in a bit, but for now, let's just go with it. This is similar to how
|
||||
when we write a generic function, we don't know what type the argument would
|
||||
be, but it's for the scope of a reference, rather than a type.
|
||||
|
||||
## But why?
|
||||
### But Why?
|
||||
|
||||
So why do we need to declare lifetimes on functions, but not inside of them?
|
||||
Here's our example from before:
|
||||
@@ -272,7 +272,7 @@ That parameter, `'a`, will represent `'r1` in the first case, and `'r2` in the
|
||||
second. Two different scopes, one single parameter. Hence, generic, just like
|
||||
two different types, one type parameter.
|
||||
|
||||
## Two lifetimes, intertwined
|
||||
### Two Lifetimes, Intertwined
|
||||
|
||||
To illustrate this principle a bit further, let's look at a function that takes
|
||||
_two_ references:
|
||||
@@ -318,7 +318,7 @@ lifetime syntax is about: connecting the lifetimes of various parameters and
|
||||
return values of functions, so that Rust can understand how long your
|
||||
references are supposed to live.
|
||||
|
||||
## Lifetime elision
|
||||
### Lifetime Elision
|
||||
|
||||
If every reference has a lifetime, and we need to provide them for functions
|
||||
that take references, then why does this function compile?
|
||||
@@ -374,7 +374,7 @@ they cover the vast majority of cases, allowing you to write a lot of code
|
||||
without needing to consider explicit lifetimes. But it's not 100% of the time,
|
||||
and so you may see explicit lifetimes used in various places.
|
||||
|
||||
## The static lifetime
|
||||
### The Static Lifetime
|
||||
|
||||
There is _one_ special lifetime that Rust knows about. Remember this program
|
||||
from before?
|
||||
|
||||
Reference in New Issue
Block a user