Skip to content

Commit

Permalink
Merge pull request #2363 from rust-lang/gh1761
Browse files Browse the repository at this point in the history
Tweak example in chapter 10
  • Loading branch information
steveklabnik authored Jun 7, 2020
2 parents f20232b + 43565d3 commit 48b9625
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ANCHOR: here
fn largest(list: &[i32]) -> i32 {
let mut largest = list[0];
fn largest(list: &[i32]) -> &i32 {
let mut largest = &list[0];

for &item in list {
for item in list {
if item > largest {
largest = item;
}
Expand All @@ -17,15 +17,15 @@ fn main() {
let result = largest(&number_list);
println!("The largest number is {}", result);
// ANCHOR_END: here
assert_eq!(result, 100);
assert_eq!(result, &100);
// ANCHOR: here

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

let result = largest(&number_list);
println!("The largest number is {}", result);
// ANCHOR_END: here
assert_eq!(result, 6000);
assert_eq!(result, &6000);
// ANCHOR: here
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ANCHOR: here
fn largest_i32(list: &[i32]) -> i32 {
let mut largest = list[0];
fn largest_i32(list: &[i32]) -> &i32 {
let mut largest = &list[0];

for &item in list {
for item in list {
if item > largest {
largest = item;
}
Expand All @@ -11,10 +11,10 @@ fn largest_i32(list: &[i32]) -> i32 {
largest
}

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

for &item in list {
for item in list {
if item > largest {
largest = item;
}
Expand All @@ -29,15 +29,15 @@ fn main() {
let result = largest_i32(&number_list);
println!("The largest number is {}", result);
// ANCHOR_END: here
assert_eq!(result, 100);
assert_eq!(result, &100);
// ANCHOR: here

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

let result = largest_char(&char_list);
println!("The largest char is {}", result);
// ANCHOR_END: here
assert_eq!(result, 'y');
assert_eq!(result, &'y');
// ANCHOR: here
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn largest<T>(list: &[T]) -> T {
fn largest<T>(list: &[T]) -> &T {
let mut largest = list[0];

for &item in list {
for item in list {
if item > largest {
largest = item;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ch10-01-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ to declare the type parameter name before we use it. To define the generic
between the name of the function and the parameter list, like this:

```rust,ignore
fn largest<T>(list: &[T]) -> T {
fn largest<T>(list: &[T]) -> &T {
```

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

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

0 comments on commit 48b9625

Please sign in to comment.