Skip to content

Commit 43565d3

Browse files
committed
Tweak example in chapter 10
We were cheating due to Copy, and that made this example awkward. Returning a reference is probably better here anyway, and it makes everything flow a bit nicer. Fixes #1761
1 parent d899c0f commit 43565d3

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ANCHOR: here
2-
fn largest(list: &[i32]) -> i32 {
3-
let mut largest = list[0];
2+
fn largest(list: &[i32]) -> &i32 {
3+
let mut largest = &list[0];
44

5-
for &item in list {
5+
for item in list {
66
if item > largest {
77
largest = item;
88
}
@@ -17,15 +17,15 @@ fn main() {
1717
let result = largest(&number_list);
1818
println!("The largest number is {}", result);
1919
// ANCHOR_END: here
20-
assert_eq!(result, 100);
20+
assert_eq!(result, &100);
2121
// ANCHOR: here
2222

2323
let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];
2424

2525
let result = largest(&number_list);
2626
println!("The largest number is {}", result);
2727
// ANCHOR_END: here
28-
assert_eq!(result, 6000);
28+
assert_eq!(result, &6000);
2929
// ANCHOR: here
3030
}
3131
// ANCHOR_END: here
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ANCHOR: here
2-
fn largest_i32(list: &[i32]) -> i32 {
3-
let mut largest = list[0];
2+
fn largest_i32(list: &[i32]) -> &i32 {
3+
let mut largest = &list[0];
44

5-
for &item in list {
5+
for item in list {
66
if item > largest {
77
largest = item;
88
}
@@ -11,10 +11,10 @@ fn largest_i32(list: &[i32]) -> i32 {
1111
largest
1212
}
1313

14-
fn largest_char(list: &[char]) -> char {
15-
let mut largest = list[0];
14+
fn largest_char(list: &[char]) -> &char {
15+
let mut largest = &list[0];
1616

17-
for &item in list {
17+
for item in list {
1818
if item > largest {
1919
largest = item;
2020
}
@@ -29,15 +29,15 @@ fn main() {
2929
let result = largest_i32(&number_list);
3030
println!("The largest number is {}", result);
3131
// ANCHOR_END: here
32-
assert_eq!(result, 100);
32+
assert_eq!(result, &100);
3333
// ANCHOR: here
3434

3535
let char_list = vec!['y', 'm', 'a', 'q'];
3636

3737
let result = largest_char(&char_list);
3838
println!("The largest char is {}", result);
3939
// ANCHOR_END: here
40-
assert_eq!(result, 'y');
40+
assert_eq!(result, &'y');
4141
// ANCHOR: here
4242
}
4343
// ANCHOR_END: here

listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
fn largest<T>(list: &[T]) -> T {
1+
fn largest<T>(list: &[T]) -> &T {
22
let mut largest = list[0];
33

4-
for &item in list {
4+
for item in list {
55
if item > largest {
66
largest = item;
77
}

src/ch10-01-syntax.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ to declare the type parameter name before we use it. To define the generic
4444
between the name of the function and the parameter list, like this:
4545

4646
```rust,ignore
47-
fn largest<T>(list: &[T]) -> T {
47+
fn largest<T>(list: &[T]) -> &T {
4848
```
4949

5050
We read this definition as: the function `largest` is generic over some type
5151
`T`. This function has one parameter named `list`, which is a slice of values
52-
of type `T`. The `largest` function will return a value of the same type `T`.
52+
of type `T`. The `largest` function will return a reference to a value of the
53+
same type `T`.
5354

5455
Listing 10-5 shows the combined `largest` function definition using the generic
5556
data type in its signature. The listing also shows how we can call the function

0 commit comments

Comments
 (0)