use serde::{Deserialize, Serialize};
use serde_json::Result;
#[derive(Serialize, Deserialize)]
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
fn typed_example() -> Result<()> {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
// Parse the string of data into a Person object. This is exactly the
// same function as the one that produced serde_json::Value above, but
// now we are asking it for a Person as output.
let p: Person = serde_json::from_str(data)?;
// Do things just like with any other Rust data structure.
println!("Please call {} at the number {}", p.name, p.phones[0]);
Ok(())
}
fn main() {
typed_example().unwrap();
}
Is there a question?
Hello and welcome! It looks like you've found the option to share code in the Playground. Feel free to ask any questions about some Rust code you may have (or just create new topics here on the forum itself).
Please also check out the pinned topics on the main page.
3 Likes
The question might be:
What does it have to do with sha256? But I have no answer.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.