I heard that a structure is necessary to load toml in trust, but how can I tell you about the flexibility of the model?
use service_derive::Deserialize;
# [derive (Deserialize)]
US>structure User {
user:string<-- I want to be able to do numbers as well
}
US>fnmain(){
let user: User=toml::from_str(r#"
user=1234
US>"#).unwrap()
);
}
The TOML specification interprets it as an integer type, so it should be enclosed in "
.
let user:User=toml::from_str(r#"user="1234""#).unwrap();
Or you can use enum to write as follows:
use service::Deserialize;
# [derive (Deserialize, Debug)]
#[serde(untagged)]
enum User {
ByName {user:String},
ByNumber {user:i32},
}
US>fnmain(){
let user1: User=toml::from_str(r#"user=1234"#).unwrap();
let user2:User=toml::from_str(r#"user="foo""#).unwrap();
println!("{:?}, {:?}", user1, user2)
}
ByNumber {user:1234}, ByName {user:"foo"}
© 2024 OneMinuteCode. All rights reserved.