-2

I'm doing a poker assignment in which a base of 2 hands is given and my goal is to figure out in each hand what the highest card is. I've tried to pass in the reference to each array to another function that is supposed to figure it out:

fn highest_card(c: &[(i32, char)], d: &[(i32, char)]) {
    let mut max = 0;
    let mut m: usize = 0;
    let n: usize = 0;

    while m < c.len() {
        if c[m].n > max {
            max = c[m].n;
        }
        m += 1;
    }
    println!("The max number is: {}", max);
}

fn main() {
    let hand1 = [(3, 'H'), (10, 'S'), (4, 'S'), (4, 'C'), (5, 'C')];
    let hand2 = [(2, 'H'), (2, 'S'), (5, 'S'), (2, 'C'), (13, 'C')];
    let xxx = highest_card(&hand1, &hand2);
}

When running this, it displays the error:

error[E0609]: no field `n` on type `(i32, char)`
 --> src/main.rs:7:17
  |
7 |         if c[m].n > max {
  |                 ^

error[E0609]: no field `n` on type `(i32, char)`
 --> src/main.rs:8:24
  |
8 |             max = c[m].n;
  |                        ^

What should I do to address this issue?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    Welcome to Stack Overflow! You should copy and paste the error text into your post, not only to sidestep the low-rep restriction but also to make it searchable for future people who have similar questions. – trent Jun 17 '18 at 00:55

1 Answers1

2

You should start by re-reading The Rust Programming Language, specifically the section about the tuple data type.

Tuple fields do not have names, that's what makes them a tuple. Thus, you cannot have a field called n on a tuple. That's what the error means.

You cannot store the tuple index in a variable, either.

You need to type the literal characters .0 to access the field:

c[m].0

See also:


You should also read the chapter on iterators and become familiar with all the methods on Iterator.

I'd recommend learning patterns as well.

I'd write this code as:

fn highest_card(c: &[(i32, char)]) {
    let max = c.iter().max_by_key(|(val, _suit)| val);
    println!("The max card is: {:?}", max);
}

Think hard about why the type of the variable max is an Option.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366