O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercises

  1. Why did println! need two patterns?

Solution: The first pattern of println! matches a call where there is just the format string and no parameters. Without it, the following would match no rules:

println!("Hello world!") 
  1. Why is println! a macro instead of just a function?

Solution: Having println! as a macro gives it the power of compile-time checking of its parameters. A print statement with a wrong number of arguments could never work, so it's nice that it fails at compile time. Furthermore, Rust does not have variable arguments (at least yet), so you would have to pass format arguments in a list or similar structure.

  1. Think about your second favorite compiled programming language. How does it do the same checks that println! ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required