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. Write a trait, Serializable, with methods ser and deser. Create a custom derive attribute using macros 1.1 that implements those functions automatically. You don't have to be able to load and save every kind of type, just a few primitives will be more than fine.

Solution: This was a deliberately open-ended question with vague specs. Here's possibly the simplest implementation:

trait Serializable {     fn ser(&self) -> String;     fn deser(s: &str) -> Self; }  impl Serializable for u32 {     fn ser(&self) -> String {         self.to_string()     }      fn deser(s: &str) -> Self {         s.parse().expect("Deserializing number failed")     } } 
  1. Write a compiler plugin that disallows functions that are too long.

Solution: Here's an implementation that just counts ...

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