diff --git a/ch01-01-introduction.html b/ch01-01-introduction.html index 06e3ead02..b86c67f8b 100644 --- a/ch01-01-introduction.html +++ b/ch01-01-introduction.html @@ -47,7 +47,7 @@
diff --git a/ch01-02-installation.html b/ch01-02-installation.html index 063db757a..774a12e9d 100644 --- a/ch01-02-installation.html +++ b/ch01-02-installation.html @@ -47,7 +47,7 @@
diff --git a/ch01-03-hello-world.html b/ch01-03-hello-world.html index 350e1107d..50be24243 100644 --- a/ch01-03-hello-world.html +++ b/ch01-03-hello-world.html @@ -47,7 +47,7 @@
diff --git a/ch01-04-design.html b/ch01-04-design.html index 74e505a2f..bae3d70c7 100644 --- a/ch01-04-design.html +++ b/ch01-04-design.html @@ -47,7 +47,7 @@
diff --git a/ch03-01-up-and-running.html b/ch03-01-up-and-running.html index 1841e9028..e04231a08 100644 --- a/ch03-01-up-and-running.html +++ b/ch03-01-up-and-running.html @@ -47,7 +47,7 @@
diff --git a/ch03-02-variable-bindings.html b/ch03-02-variable-bindings.html index aff93a64a..2116b56ae 100644 --- a/ch03-02-variable-bindings.html +++ b/ch03-02-variable-bindings.html @@ -47,7 +47,7 @@
diff --git a/ch03-03-functions.html b/ch03-03-functions.html index f078b6cc7..ca367c3b9 100644 --- a/ch03-03-functions.html +++ b/ch03-03-functions.html @@ -47,7 +47,7 @@
diff --git a/ch03-04-scalar-types.html b/ch03-04-scalar-types.html index c7eebbf5d..3bd20268d 100644 --- a/ch03-04-scalar-types.html +++ b/ch03-04-scalar-types.html @@ -47,7 +47,7 @@
diff --git a/ch03-05-compound-types.html b/ch03-05-compound-types.html index 16d83847e..f398db980 100644 --- a/ch03-05-compound-types.html +++ b/ch03-05-compound-types.html @@ -47,7 +47,7 @@
diff --git a/ch03-06-comments.html b/ch03-06-comments.html index c0a3e18a4..df6fb576b 100644 --- a/ch03-06-comments.html +++ b/ch03-06-comments.html @@ -47,7 +47,7 @@
diff --git a/ch03-07-if.html b/ch03-07-if.html index dfa1db263..77a07fe98 100644 --- a/ch03-07-if.html +++ b/ch03-07-if.html @@ -47,7 +47,7 @@
diff --git a/ch03-08-loops.html b/ch03-08-loops.html index 8077f1896..fec62e79d 100644 --- a/ch03-08-loops.html +++ b/ch03-08-loops.html @@ -47,7 +47,7 @@
diff --git a/ch04-01-understanding-ownership.html b/ch04-01-understanding-ownership.html index 1d36aad8f..14edb394f 100644 --- a/ch04-01-understanding-ownership.html +++ b/ch04-01-understanding-ownership.html @@ -47,7 +47,7 @@
diff --git a/ch04-02-ownership.html b/ch04-02-ownership.html index 3d54da2e7..89c8a88ea 100644 --- a/ch04-02-ownership.html +++ b/ch04-02-ownership.html @@ -47,7 +47,7 @@
diff --git a/ch04-03-references-and-borrowing.html b/ch04-03-references-and-borrowing.html index d34954dc9..0c575ad5a 100644 --- a/ch04-03-references-and-borrowing.html +++ b/ch04-03-references-and-borrowing.html @@ -47,7 +47,7 @@
diff --git a/ch04-04-slices.html b/ch04-04-slices.html index dbdb4addc..60e1ee9d9 100644 --- a/ch04-04-slices.html +++ b/ch04-04-slices.html @@ -47,7 +47,7 @@
diff --git a/ch05-01-structs.html b/ch05-01-structs.html index 904341731..9968aa29a 100644 --- a/ch05-01-structs.html +++ b/ch05-01-structs.html @@ -47,7 +47,7 @@
diff --git a/ch05-02-method-syntax.html b/ch05-02-method-syntax.html index ed5faa363..5f96235c0 100644 --- a/ch05-02-method-syntax.html +++ b/ch05-02-method-syntax.html @@ -47,7 +47,7 @@
@@ -301,7 +301,7 @@ method.

- @@ -315,7 +315,7 @@ method.

- diff --git a/ch05-03-generics.html b/ch05-03-generics.html deleted file mode 100644 index 742ad233c..000000000 --- a/ch05-03-generics.html +++ /dev/null @@ -1,117 +0,0 @@ - - - - - The Rust Programming Language - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - -
-

Generics

- -
- - - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - - - - diff --git a/ch06-01-enums.html b/ch06-01-enums.html index 2db029d5a..3806fb975 100644 --- a/ch06-01-enums.html +++ b/ch06-01-enums.html @@ -47,7 +47,7 @@
@@ -184,7 +184,7 @@ useful with enums: match.

- @@ -198,7 +198,7 @@ useful with enums: match.

- diff --git a/ch06-02-option.html b/ch06-02-option.html index 4e9c09c58..a93573478 100644 --- a/ch06-02-option.html +++ b/ch06-02-option.html @@ -47,7 +47,7 @@
diff --git a/ch06-03-match.html b/ch06-03-match.html index b3e9a2d64..79f233c8e 100644 --- a/ch06-03-match.html +++ b/ch06-03-match.html @@ -47,7 +47,7 @@
diff --git a/ch06-04-if-let.html b/ch06-04-if-let.html index d3b5f3e98..1ed59906d 100644 --- a/ch06-04-if-let.html +++ b/ch06-04-if-let.html @@ -47,7 +47,7 @@
diff --git a/chXX-patterns.html b/chXX-patterns.html index e2ff20ef0..5e40d74bb 100644 --- a/chXX-patterns.html +++ b/chXX-patterns.html @@ -47,7 +47,7 @@
diff --git a/chYY-YY-documentation.html b/chYY-YY-documentation.html index 17e7746fd..90ab400e1 100644 --- a/chYY-YY-documentation.html +++ b/chYY-YY-documentation.html @@ -47,7 +47,7 @@
diff --git a/chZZ-generics.html b/chZZ-generics.html index 929040956..ef1cbf5d8 100644 --- a/chZZ-generics.html +++ b/chZZ-generics.html @@ -47,7 +47,7 @@
@@ -68,6 +68,148 @@

Generics

+

We've been working with a Point struct that looks like this:

+
#[derive(Debug,Copy,Clone)]
+struct Point {
+    x: f64,
+    y: f64,
+}
+
+

But what if we didn't want to always use an f64 here? What about an f32 for +when we need less precision? Or an i32 if we only want integer coordinates?

+

While our simple Point struct may be a bit too simple to bother making +generic in a real application, we're going to stick with it to show you the +syntax. Especially when building library code, generics allow for more code +re-use, and unlock a lot of powerful techniques.

+

Generic data types

+

'Generics' let us write code that allows for several different types, while +letting us have one definition. A more generic Point would look like this:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T> {
+    x: T,
+    y: T,
+}
+
+

There are two changes here, and they both involve this new T. The first change +is in the definition:

+
# #[derive(Debug,Copy,Clone)]
+struct Point<T> {
+#     x: T,
+#     y: T,
+# }
+
+

Our previous definition said, "We are defining a struct named Point." This +definition says something slightly different: "We are defining a struct named +Point with one type parameter T."

+

Let's talk about this term 'type parameter'. We've already seen one other thing +called a 'parameter' in Rust: function parameters:

+
fn plus_one(x: i32) -> i32 {
+    x + 1
+}
+
+

Here, x is a parameter to this function. We can call this function with a +different value, and x will change each time it's called:

+
# fn plus_one(x: i32) -> i32 {
+#     x + 1
+# }
+let six = plus_one(5);
+let eleven = plus_one(10);
+
+

In the same way, a type parameter allows us to define a data type which can be +different each time we use it:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T> {
+    x: T,
+    y: T,
+}
+
+let integral_point = Point { x: 5, y: 5 };
+let floating_point = Point { x: 5.0, y: 5.0 };
+
+

Here, integral_point uses i32 values for T, and floating_point uses +f64 values. This also leads us to talk about the second change we made to Point:

+
# #[derive(Debug,Copy,Clone)]
+# struct Point<T> {
+    x: T,
+    y: T,
+# }
+
+

Instead of saying x: i32, we say x: T. This T is the same one that we +used above in the struct declaration. Because x and y both use T, they'll +be the same type. We could give them different types:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T, OtherT> {
+    x: T,
+    y: OtherT,
+}
+
+let different = Point { x: 5, y: 5.0 };
+let same = Point { x: 5.0, y: 5.0 };
+
+

Here, instead of a single parameter, T, we have two: T and OtherT. Type +parameters have the same naming convention as other types: CamelCase. +However, you'll often see short, one-letter names used for types. T is very +common, because it's short for 'type', but you can name them something longer +if you'd like. In this version of Point, we say that x has the type T, +and y has the type OtherT. This lets us give them two different types, but +they don't have to be.

+

Generic functions

+

Regular old functions can also take generic parameters, with a syntax that looks +very similar:

+
fn foo<T>(x: T) {
+    // ...
+}
+
+

This foo() function has one generic parameter, T, and takes one argument, +x, which has the type T. Let's talk a little bit more about what this means.

+

Generic methods

+

We've seen how to define methods with the impl keyword. Our generic Point +can have generic methods, too:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T> {
+    x: T,
+    y: T,
+}
+
+impl<T> Point<T> {
+    fn some_method(&self) {
+        // ...
+    }
+}
+
+

We also need the <T> after impl. This line reads, "We will be implementing +methods with one generic type parameter, T, for a type, Point, which takes +one generic type T." In a sense, the impl<T> says "we will be using a type +T" and the Point<T> says "that T is used for Point." In this simple +case, this syntax can feel a bit redundant, but when we get into some of Rust's +more advanced features later, this distinction will become more useful.

+

There's more to the story

+

This section covered the basic syntax of generics, but it's not the full story. +For example, let's try to implement our foo() function: we'll have it print out +the value of x:

+
fn foo<T>(x: T) {
+    println!("x is: {}", x);
+}
+
+

We'll get an error:

+
error: the trait `core::fmt::Display` is not implemented for the type `T` [E0277]
+println!("x is: {}", x);
+                     ^
+
+

We can't print out x! The error messages reference something we talked about +briefly before, the Display trait. In order to implement this function, we +need to talk about traits. But we only need to talk about traits to implement +our own generic functions; we don't need this understanding to use them. So +rather than get into more details about this right now, let's talk about other +useful Rust data types, and we can come back to implementing generic functions +in the chapter about traits.

+

For now, the important bits to understand:

+
    +
  • Generic type parameters are kind of like function parameters, but for types +instead of values.
  • +
  • Type parameters go inside <>s and are usually named things like T.
  • +
+

With that, let's talk about another fundamental Rust data type: enums.

diff --git a/index.html b/index.html index ab9defcc6..9dd68927f 100644 --- a/index.html +++ b/index.html @@ -46,7 +46,7 @@
diff --git a/print.html b/print.html index 0d0920c4a..ef2f774ed 100644 --- a/print.html +++ b/print.html @@ -47,7 +47,7 @@
@@ -3018,7 +3018,6 @@ we must pass in &p1 explicitly.

While methods can be called like functions, functions cannot be called like methods. If the first argument isn’t named self, it cannot be called like a method.

-

Generics

Enums

Next, let’s look at a feature of Rust that’s similar to structs, but also different. Enumerations, or ‘enums’ as they’re more commonly referred to, @@ -3399,6 +3398,148 @@ match expression {

In other words, it's the high-level construct we were originally looking for: do something with a single pattern.

Generics

+

We've been working with a Point struct that looks like this:

+
#[derive(Debug,Copy,Clone)]
+struct Point {
+    x: f64,
+    y: f64,
+}
+
+

But what if we didn't want to always use an f64 here? What about an f32 for +when we need less precision? Or an i32 if we only want integer coordinates?

+

While our simple Point struct may be a bit too simple to bother making +generic in a real application, we're going to stick with it to show you the +syntax. Especially when building library code, generics allow for more code +re-use, and unlock a lot of powerful techniques.

+

Generic data types

+

'Generics' let us write code that allows for several different types, while +letting us have one definition. A more generic Point would look like this:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T> {
+    x: T,
+    y: T,
+}
+
+

There are two changes here, and they both involve this new T. The first change +is in the definition:

+
# #[derive(Debug,Copy,Clone)]
+struct Point<T> {
+#     x: T,
+#     y: T,
+# }
+
+

Our previous definition said, "We are defining a struct named Point." This +definition says something slightly different: "We are defining a struct named +Point with one type parameter T."

+

Let's talk about this term 'type parameter'. We've already seen one other thing +called a 'parameter' in Rust: function parameters:

+
fn plus_one(x: i32) -> i32 {
+    x + 1
+}
+
+

Here, x is a parameter to this function. We can call this function with a +different value, and x will change each time it's called:

+
# fn plus_one(x: i32) -> i32 {
+#     x + 1
+# }
+let six = plus_one(5);
+let eleven = plus_one(10);
+
+

In the same way, a type parameter allows us to define a data type which can be +different each time we use it:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T> {
+    x: T,
+    y: T,
+}
+
+let integral_point = Point { x: 5, y: 5 };
+let floating_point = Point { x: 5.0, y: 5.0 };
+
+

Here, integral_point uses i32 values for T, and floating_point uses +f64 values. This also leads us to talk about the second change we made to Point:

+
# #[derive(Debug,Copy,Clone)]
+# struct Point<T> {
+    x: T,
+    y: T,
+# }
+
+

Instead of saying x: i32, we say x: T. This T is the same one that we +used above in the struct declaration. Because x and y both use T, they'll +be the same type. We could give them different types:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T, OtherT> {
+    x: T,
+    y: OtherT,
+}
+
+let different = Point { x: 5, y: 5.0 };
+let same = Point { x: 5.0, y: 5.0 };
+
+

Here, instead of a single parameter, T, we have two: T and OtherT. Type +parameters have the same naming convention as other types: CamelCase. +However, you'll often see short, one-letter names used for types. T is very +common, because it's short for 'type', but you can name them something longer +if you'd like. In this version of Point, we say that x has the type T, +and y has the type OtherT. This lets us give them two different types, but +they don't have to be.

+

Generic functions

+

Regular old functions can also take generic parameters, with a syntax that looks +very similar:

+
fn foo<T>(x: T) {
+    // ...
+}
+
+

This foo() function has one generic parameter, T, and takes one argument, +x, which has the type T. Let's talk a little bit more about what this means.

+

Generic methods

+

We've seen how to define methods with the impl keyword. Our generic Point +can have generic methods, too:

+
#[derive(Debug,Copy,Clone)]
+struct Point<T> {
+    x: T,
+    y: T,
+}
+
+impl<T> Point<T> {
+    fn some_method(&self) {
+        // ...
+    }
+}
+
+

We also need the <T> after impl. This line reads, "We will be implementing +methods with one generic type parameter, T, for a type, Point, which takes +one generic type T." In a sense, the impl<T> says "we will be using a type +T" and the Point<T> says "that T is used for Point." In this simple +case, this syntax can feel a bit redundant, but when we get into some of Rust's +more advanced features later, this distinction will become more useful.

+

There's more to the story

+

This section covered the basic syntax of generics, but it's not the full story. +For example, let's try to implement our foo() function: we'll have it print out +the value of x:

+
fn foo<T>(x: T) {
+    println!("x is: {}", x);
+}
+
+

We'll get an error:

+
error: the trait `core::fmt::Display` is not implemented for the type `T` [E0277]
+println!("x is: {}", x);
+                     ^
+
+

We can't print out x! The error messages reference something we talked about +briefly before, the Display trait. In order to implement this function, we +need to talk about traits. But we only need to talk about traits to implement +our own generic functions; we don't need this understanding to use them. So +rather than get into more details about this right now, let's talk about other +useful Rust data types, and we can come back to implementing generic functions +in the chapter about traits.

+

For now, the important bits to understand:

+
    +
  • Generic type parameters are kind of like function parameters, but for types +instead of values.
  • +
  • Type parameters go inside <>s and are usually named things like T.
  • +
+

With that, let's talk about another fundamental Rust data type: enums.

Patterns

We've actually used patterns a few times so far: they're used in let bindings, in function arguments, and in the match expression. Patterns have a