I am new to rust and working on a tool which interacts with the Binance API. The API returns a response like so:
{
"lastUpdateId": 1027024,
"bids": [
[
"4.00000000", // PRICE
"431.00000000" // QTY
]
],
"asks": [
[
"4.00000200",
"12.00000000"
]
]
}
Bids and Asks are an array of arrays. I have to declare the struct
for the API response.
I currently have
use reqwest::Error;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct Depth {
lastUpdateId: u32,
bids: Vec<u64>,
asks: Vec<u64>,
}
#[tokio::main]
pub async fn order_book() -> Result<(), Error> {
let request_url = format!("https://api.binance.us/api/v3/depth?symbol=BTCUSD");
println!("{}", request_url);
let response = reqwest::get(&request_url).await?;
println!("Status: {}", response.status());
let depth: Depth = response.json().await?;
println!("{:?}", depth);
Ok(())
}
I believe I am declaring the type for bids and asks incorrectly but I am unable to determine how to declare an array of arrays. I get a response.status of 200, but I am unable to print out the response.json
.
Thank You!
Since it returns an array of arrays, you need to nest the types.
bids: Vec<Vec<u64>>
should work, but since you know each inner array will have two elements, you can make it more efficient withbids: Vec<[u64; 2]>
- a dynamically-sized vector of fixed-size arrays, orbids: Vec<(u64, u64)>
, a dynamically-sized vector of tuples. You'd also need to updateasks
similarly.Your final code could look something like this: