2

So I have gone through 90% of the tutorial on Rust and I think I mostly have a grasp on the syntax. I'm attempting to start writing code with it I'm currently using the rustc_serialize library to parse JSON from stdin and I'm not getting the results I expect. I have the following JSON file called message.txt the following content:

{"text": "hello world"}

Here is the Rust code to accept stdin and parse out the text field:

extern crate rustc_serialize;

use std::io::{self, Read};
use rustc_serialize::json::Json;

fn main() {
    // provide a buffer for stdin
    let mut buffer = String::new();
    let _ = io::stdin().read_to_string(&mut buffer);

    // parse the json
    let message = match Json::from_str(&mut buffer) {
        Ok(m) => m,
        Err(_) => panic!("Stdin provided invalid JSON")
    };

    // get the message object and "text" field string
    let message_object = message.as_object().unwrap();
    let message_string = message_object.get("text").unwrap();

    println!("{}", message_string);
    println!("{}", &message_string.to_string()[0..4]);
}

The following code outputs:

"Hello World"
"Hel

I'm currently outputting the byte slice to make sure the quote wasn't something that was added by print. According to the docs message_string shouldn't have quotes around it.

If I print out the data using the example from the documentation then it prints the value of "text" without quotes:

for (key, value) in message_object.iter() {
    println!("{}: {}", key, match *value {
        Json::U64(v) => format!("{} (u64)", v),
        Json::String(ref v) => format!("{} (string)", v),
        _ => format!("other")
    });
}

Output:

text: hello world (string)

I'm a newbie to Rust so I probably just don't understand the string manipulation parts of Rust all that well.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
glenbot
  • 685
  • 6
  • 7

1 Answers1

8

The problem is that message_string isn't what you think it is. I discovered that when I tried to use len on the "string", which didn't work (I assume that's why you have a to_string when you are slicing). Let's make the compiler tell us what it is:

let () = message_string;

Has the error:

error: mismatched types:
 expected `&rustc_serialize::json::Json`,
    found `()`

It's a Json! We need to convert that enumerated type into a string-like thing:

let message_object = message.as_object().unwrap();
let message_json = message_object.get("text").unwrap();
let message_string = message_json.as_string().unwrap();

Ultimately, I'd argue that Display (which allows the {} format string) should not have been implemented for this type, as Display means format in an end-user-focused manner. It's probably too late to change that decision now though.


I know that unwrap is great for quick prototyping, but I'd be remiss in not showing a slightly more idiomatic way of doing this:

fn main() {
    let mut buffer = String::new();
    io::stdin().read_to_string(&mut buffer).expect("Could not read from stdin");

    let message = Json::from_str(&mut buffer).expect("Stdin provided invalid JSON");

    let message_string = message.as_object().and_then(|obj| {
        obj.get("text").and_then(|json| {
            json.as_string()
        })
    }).expect("The `text` key was missing or not a string");

    println!("{}", message_string);
}

Ignoring the Result from read_to_string is worse than panicking. ^_^

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    `let () =` trick is great, this is probably well-known but I am starting in Rust as well, and this is handy. – Xavier T. Dec 11 '15 at 16:50
  • 1
    `let () =` is an amazing trick! Thank you! Also, really appreciate the idiomatic syntax and the great explanation. When you say `Display` do mean [this trait](https://doc.rust-lang.org/std/fmt/trait.Display.html)? – glenbot Dec 11 '15 at 17:51
  • @glenbot yup. I've edited that link into my answer too. :-) – Shepmaster Dec 11 '15 at 18:26