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
Just like how we had `Option<T>`, we use a similar syntax for `inverse<T>`.
4916
-
We can then use `T` inside the rest of the signature: `x` has type `T`, and half
4917
-
of the `Result` has type `T`. However, if we try to compile that example, we'll get
4918
-
an error:
4916
+
Just like how we had `Option<T>`, we use a similar syntax for `inverse<T>`. We
4917
+
can then use `T` inside the rest of the signature: `x` has type `T`, and half of
4918
+
the `Result` has type `T`. However, if we try to compile that example, we'll get
4919
+
some errors:
4919
4920
4920
4921
```text
4921
4922
error: binary operation `==` cannot be applied to type`T`
4923
+
if x == 0.0 { return Err("x cannot be zero!".to_string()); }
4924
+
^~~~~~~~
4925
+
error: mismatched types: expected `_`, found `T` (expected floating-point variable, found type parameter)
4926
+
Ok(1.0 / x)
4927
+
^
4928
+
error: mismatched types: expected `core::result::Result<T, collections::string::String>`, found `core::result::Result<_, _>` (expected type parameter, found floating-point variable)
4929
+
Ok(1.0 / x)
4930
+
^~~~~~~~~~~
4922
4931
```
4923
4932
4924
-
Because `T` can be _any_ type, it may be a type that doesn't implement `==`,
4925
-
and therefore, the first line would be wrong. What do we do?
4933
+
The problem is that `T` is unconstrained: it can be _any_ type. It could be a
4934
+
`String`, and the expression `1.0 / x` has no meaning if`x` is a `String`. It
4935
+
may be a type that doesn't implement `==`, and the first line would be
4936
+
wrong. What do we do?
4926
4937
4927
-
To fix this example, we need to learn about another Rust feature: traits.
4938
+
To fix this example, we need to learn about another Rust feature: **traits**.
4928
4939
4929
4940
# Traits
4930
4941
4931
-
Do you remember the `impl` keyword, used to call a function with method
4932
-
syntax?
4942
+
Our discussion of **traits** begins with the `impl` keyword. We used it before
4943
+
to specify methods.
4933
4944
4934
4945
```{rust}
4935
4946
struct Circle {
@@ -4945,8 +4956,8 @@ impl Circle {
4945
4956
}
4946
4957
```
4947
4958
4948
-
Traits are similar, except that we define a trait with just the method
4949
-
signature, then implement the trait for that struct. Like this:
4959
+
We define a trait in terms of its methods. We then `impl` a trait `for` a type
4960
+
(or many types).
4950
4961
4951
4962
```{rust}
4952
4963
struct Circle {
@@ -4966,19 +4977,18 @@ impl HasArea for Circle {
4966
4977
}
4967
4978
```
4968
4979
4969
-
As you can see, the `trait` block looks very similar to the `impl` block,
4970
-
but we don't define a body, just a type signature. When we `impl` a trait,
4971
-
we use `impl Trait for Item`, rather than just `impl Item`.
4980
+
The `trait` block defines only type signatures. When we `impl` a trait, we use
4981
+
`impl Trait for Item`, rather than just `impl Item`.
4972
4982
4973
-
So what's the big deal? Remember the error we were getting with our generic
4974
-
`inverse` function?
4983
+
The first of the three errors we got with our generic `inverse` function was
4984
+
this:
4975
4985
4976
4986
```text
4977
4987
error: binary operation `==` cannot be applied to type `T`
4978
4988
```
4979
4989
4980
-
We can use traits to constrain our generics. Consider this function, which
4981
-
does not compile, and gives us a similar error:
4990
+
We can use traits to constrain generic type parameters. Consider this function,
4991
+
which does not compile, and gives us a similar error:
4982
4992
4983
4993
```{rust,ignore}
4984
4994
fn print_area<T>(shape: T) {
@@ -4993,8 +5003,9 @@ error: type `T` does not implement any method in scope named `area`
4993
5003
```
4994
5004
4995
5005
Because `T` can be any type, we can't be sure that it implements the `area`
4996
-
method. But we can add a **trait constraint** to our generic `T`, ensuring
4997
-
that it does:
5006
+
method. But we can add a **trait constraint** to our generic `T`, ensuring that
5007
+
we can only compile the functionif it's called with types which `impl` the
0 commit comments