Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 11, 2018 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/6b0a85f2818998739f32c18aa814bf77 to your computer and use it in GitHub Desktop.
Save rust-play/6b0a85f2818998739f32c18aa814bf77 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![allow(dead_code, unused_variables)]
extern crate serde; // 1.0.68
#[macro_use]
extern crate serde_derive; // 1.0.68
extern crate serde_json; // 1.0.22
#[derive(Deserialize, Debug)]
struct Price {
timestamp: u64,
btc: String,
usd: f64,
}
use serde_json::Error;
// This is what the data looks like that I'll be consuming
// I'm not worried about btc being a string right now, I
// plan to deal with that, unfortunately the API owner
// has his own reasons for representing it that way so it's
// going to stay.
fn get_data() -> Result<(), Error> {
let data = r#"
[
{
"timestamp": 1531107003,
"btc": "0.00000046",
"usd": 0.0031
},
{
"timestamp": 1531106702,
"btc": "0.00000046",
"usd": 0.0031
},
{
"timestamp": 1531106402,
"btc": "0.00000046",
"usd": 0.0031
}
]
"#;
let p: Vec<Price> = serde_json::from_str(data)?;
for i in p {
println!{"{:#?}", i.btc};
}
Ok(())
}
fn main() {
// I'm trying to massage the above data into an array
// of tuples that will resemble the one below. I already
// have all the internals worked out for selecting one
// type of currency. What I don't know how to do is
// get that Vec<Price>.btc (or .usd, etc.) into this
// structure so I can feed it to textplot, which unfortunately
// can't be extern'd into the playground. Examples can be found
// here so you know what I'm talking about.
// https://github.com/loony-bean/textplots-rs/blob/master/examples/hist.rs
let points = [
(0.0, 11.677842),
(1.0, 9.36832),
(2.0, 9.44862)];
let v = get_data();
match v {
Ok(value) => println!("working"),
Err(e) => println!("{}", e),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment