2

I am learning using rust to write a web service, now facing a problem that when I am compile my project, show warning like this:

warning: structure field `mvId` should have a snake case name
  --> src/biz/music/../../model/request/music/play_record_request.rs:18:9
   |
18 |     pub mvId: i64,
   |         ^^^^ help: convert the identifier to snake case: `mv_id`

this is the entity I am define:

#[derive(Debug, PartialEq, Eq, Deserialize,Serialize)]
pub struct PlayRecordRequest {
    pub id: i64,
    pub title: String,
    pub url: String,
    pub mvId: i64,
    pub album: Album,
    pub artist: Vec<Artist>
}

I could not rename the mvId to mv_id because the other system was named mvId so I must using mvId to receive. I have tried to find the allow annotation in rust like #[allow(non_camel_case_types)] but did not found the property annotation to handle this situation. what should I do to avoid this problem? any suggestion is grateful.

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • 2
    `#[allow(non_snake_case)]` right above the field? – Alexey S. Larionov Sep 02 '21 at 10:56
  • 1
    The error message should include the name of the warning, but it looks like you didn't include this part of the error message in your post. – Sven Marnach Sep 02 '21 at 10:57
  • thank you, it works @AlexeyLarionov but I did not know is it the best way to handle this or this way did have some side effect or not. – Dolphin Sep 02 '21 at 11:00
  • 1
    This warning is purely about keeping a consistent code style, so it shouldn't cause any actual bugs. Only code clarity suffers. Also as @SvenMarnach suggested, every warning usually comes with a tip how to solve it (or how to suppress it), so you better always read what compiler says, it's very helpful – Alexey S. Larionov Sep 02 '21 at 11:03
  • I read the question, it is another way to solve this problem. @E_net4 I did not known which one is better, Thank you for your time. I am using the annotation way to avoid it right now. – Dolphin Sep 02 '21 at 11:36
  • 2
    Disabling this warning is of course perfectly fine, but feel free to also look into the `#[serde(rename_all = ...)]` suggestion from the other questions. If it works for your use case, it will allow you to use the "rust-like" identifiers and make your structs consistent with the rest of your Rust code. – user4815162342 Sep 02 '21 at 12:53

0 Answers0