0

What is error ascription referring to in this case and how can I fix my code to println based on the arity passed in?

Running the code below throws the error:

error: expected type, found `10`
  --> src/main.rs:21:27
   |
21 |    tellTime(Clock:Sundial(10));
   |                           ^^ expecting a type here because of type ascription

main.rs:

enum Clock {
    Sundial(u8),
    Digital(u8, u8),
    Analog(u8, u8, u8),
}

fn tellTime(clock: Clock) {
    match clock {
      Clock::Sundial(hours) =>
          println!("Time is: {}", hours),
      Clock::Digital(hours, mins) =>
          println!("Time is: {}:{}", hours, mins),
      Clock::Analog(hours, mins, secs) =>
          println!("Time is: {}:{}:{}", hours, mins, secs),
      _ => println!("Not a clock!")

    }

}
fn main() {
   tellTime(Clock:Sundial(10));
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • 2
    You have a typo. You meant `Clock::Sundial(10)` — **two** colons. – Shepmaster Mar 26 '19 at 01:30
  • 1
    Idiomatic Rust uses `snake_case` for variables, methods, macros, fields and modules; `UpperCamelCase` for types and enum variants; and `SCREAMING_SNAKE_CASE` for statics and constants. Use `tell_time` instead, please. – Shepmaster Mar 26 '19 at 01:30
  • 2
    Note that this does **not** match on arity — that's not possible. This matches on the **name** of the enum variant. The variants happen to have different arity, but they don't need to. – Shepmaster Mar 26 '19 at 01:33
  • 1
    I bought your series btw ;) I'm literally going through it lol. I'm sure you noticed lol – Armeen Moon Mar 26 '19 at 01:36
  • That's wonderful! I did think to myself that the sundial example seemed familiar, but I chalked it up to just being a common example ^_^ – Shepmaster Mar 26 '19 at 01:38

0 Answers0