I am using Rust to write a REST API and my Flutter client defines the entity like this:
class WordDefinition {
String type;
String name;
List<String> values;
WordDefinition({
this.type,
this.name,
this.values,
});
}
The client uses type
as a entity field name. In Dart it works fine, but in the server side Rust I could not define the field name like this:
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct WordDefinition {
pub type: String,
pub text: String,
pub translations: Vec<String>,
}
error: expected identifier, found keyword `type`
--> src/lib.rs:5:9
|
4 | pub struct WordDefinition {
| -------------- while parsing this struct
5 | pub type: String,
| ^^^^ expected identifier, found keyword
What should I do to avoid the Rust keyword conflict? Is it possible to define a entity name using type
like this in Rust?
r#type
: play.rust-lang.org/… - or you can use a different field name and use#[serde(rename = "type")]
– Mereditype
to use it as an identifier ...pub r#type: String
". – Microbicide