rational_numbers.rs
SHA-25665c93e277cffe0310803e64c948e263d1f833bf70b6628c68497c83c0be134a7
Malecule∅
Evidence
1//! This example shows how to write your own custom implementation of
2//! `Decodable` to parse rational …
8:9… rate "rustc-serialize" as rustc_serialize;
9
10use std::str;
11
12use regex::Regex;
13use rustc_serialize::{Decodable, Decoder};
14
15#[derive(Debug)]
16struct Rational {
17 numerator: i64,
18 denominator: i64,
19}
20
21impl Decodable for Rational {
22 fn decode<D: Decoder>(d: &mut D) -> Result<Rational, D::Error> {
23 let field = try!(d.read_str());
24 // This uses the `FromStr` impl below.
25 match field.parse() {
26 Ok(rat) => Ok(rat),
27 …