@@ -982,7 +982,8 @@ The obvious approach is to define `Cons` as containing an element in the list
982
982
along with the next ` List ` node. However, this will generate a compiler error.
983
983
984
984
~~~ {.ignore}
985
- // error: illegal recursive enum type; wrap the inner value in a box to make it representable
985
+ // error: illegal recursive enum type; wrap the inner value in a box to make it
986
+ // representable
986
987
enum List {
987
988
Cons(u32, List), // an element (`u32`) and the next node in the list
988
989
Nil
@@ -1054,10 +1055,10 @@ immutable, the whole list is immutable. The memory allocation itself is the
1054
1055
box, while the owner holds onto a pointer to it:
1055
1056
1056
1057
~~~ {.notrust}
1057
- List box List box List box List box
1058
- +--------------+ +--------------+ +--------------+ +-------------- +
1059
- list -> | Cons | 1 | ~ | -> | Cons | 2 | ~ | -> | Cons | 3 | ~ | -> | Nil |
1060
- +--------------+ +--------------+ +--------------+ +-------------- +
1058
+ List box List box List box List box
1059
+ +--------------+ +--------------+ +--------------+ +----------+
1060
+ list -> | Cons | 1 | ~ | -> | Cons | 2 | ~ | -> | Cons | 3 | ~ | -> | Nil |
1061
+ +--------------+ +--------------+ +--------------+ +----------+
1061
1062
~~~
1062
1063
1063
1064
> * Note:* the above diagram shows the logical contents of the enum. The actual
@@ -1197,7 +1198,8 @@ fn eq(xs: &List, ys: &List) -> bool {
1197
1198
// If we have reached the end of both lists, they are equal.
1198
1199
(&Nil, &Nil) => true,
1199
1200
// If the current element in both lists is equal, keep going.
1200
- (&Cons(x, ~ref next_xs), &Cons(y, ~ref next_ys)) if x == y => eq(next_xs, next_ys),
1201
+ (&Cons(x, ~ref next_xs), &Cons(y, ~ref next_ys))
1202
+ if x == y => eq(next_xs, next_ys),
1201
1203
// If the current elements are not equal, the lists are not equal.
1202
1204
_ => false
1203
1205
}
@@ -1256,7 +1258,7 @@ Using the generic `List<T>` works much like before, thanks to type inference:
1256
1258
# Cons(value, ~xs)
1257
1259
# }
1258
1260
let mut xs = Nil; // Unknown type! This is a `List<T>`, but `T` can be anything.
1259
- xs = prepend(xs, 10); // The compiler infers the type of `xs` as `List<int>` from this .
1261
+ xs = prepend(xs, 10); // Here the compiler infers `xs`'s type as `List<int>`.
1260
1262
xs = prepend(xs, 15);
1261
1263
xs = prepend(xs, 20);
1262
1264
~~~
@@ -1303,7 +1305,8 @@ fn eq<T: Eq>(xs: &List<T>, ys: &List<T>) -> bool {
1303
1305
// If we have reached the end of both lists, they are equal.
1304
1306
(&Nil, &Nil) => true,
1305
1307
// If the current element in both lists is equal, keep going.
1306
- (&Cons(ref x, ~ref next_xs), &Cons(ref y, ~ref next_ys)) if x == y => eq(next_xs, next_ys),
1308
+ (&Cons(ref x, ~ref next_xs), &Cons(ref y, ~ref next_ys))
1309
+ if x == y => eq(next_xs, next_ys),
1307
1310
// If the current elements are not equal, the lists are not equal.
1308
1311
_ => false
1309
1312
}
@@ -1331,7 +1334,8 @@ impl<T: Eq> Eq for List<T> {
1331
1334
// If we have reached the end of both lists, they are equal.
1332
1335
(&Nil, &Nil) => true,
1333
1336
// If the current element in both lists is equal, keep going.
1334
- (&Cons(ref x, ~ref next_xs), &Cons(ref y, ~ref next_ys)) if x == y => next_xs == next_ys,
1337
+ (&Cons(ref x, ~ref next_xs), &Cons(ref y, ~ref next_ys))
1338
+ if x == y => next_xs == next_ys,
1335
1339
// If the current elements are not equal, the lists are not equal.
1336
1340
_ => false
1337
1341
}
0 commit comments