Struct_builder, saving datas...etc

1 Simple example:

struct Person_and_program
<'a> {
#[builder(setter(into, strip_option))]
my_name: (String,String, Option<String>),
your_name: (String,String, Option<String>)}

I want to declare smth #[builder(...)] on several fields, not to clutter up code, how can I do this?
Also I want arbitrary number of Strings(or other... integers for ex., how do this?)

...
Arb_numbers: numbes*... //It's example, i don't know)
...

2 (Simply also don't know) Suppose I want to create a little interface to interect with someone in programm, I want different languages and renaming files accordingly what I need. For simpicity, I am creating struct and some functions with HashMap, as example:

impl Datas{
    fn from_default_languages (&mut self) -> HashMap<String,String> {
        let mut data_lang = HashMap::new();
        data_lang.insert("عرب(Arabian)".to_string(),String::from("السلام عليكم"),);
        data_lang.insert("English".to_string(),String::from("Hello"));
        data_lang.insert("हिंदी(Hindi)".to_string(),String::from("नमस्ते"));
        data_lang.insert("日本人(Japanese)".to_string(),String::from("こんにちは"));
        data_lang.insert("한국어(Korean)".to_string(),String::from("안녕하세요"));
        data_lang.insert("中文(Chinese)".to_string(),String::from("你好"));
        data_lang.insert("Português".to_string(),String::from("Olá"));
        data_lang.insert("Русский".to_string(),String::from("Здравствуйте"));
        data_lang.insert("Español".to_string(),String::from("Hola"));
        //let mut some_inter = BTreeMap::<String,String>::new();
        self.languages = data_lang;}

But also If I transfer data(structure) in json, what advantages or disadvantages of this will be?
As in previous example, i thought for simplicity like this: [Arabic-> then several phrases(as values) ...
Maybe create struct with two fields and hasmap on them], but it's not so flawless style))

3 How can I save big datas(not as above) more fast and safely accordingly to rust(mysql...but as the time being I don't know anything about data base, maybe some newest you know).

4 Also I want something with std::io to interact with somebody using program, many people i suppose had already create something safe on rust (not like in documentation, as simple examples), so question: Did you know dynamic interaction using io in rust, for example modules, saving some statistic from person.
And maybe i will write several programms, and will need to correct in one, and affect the same data in related to latter ... thanks)

Here I've formatted your code blocks to make them syntactically correct and easier to read:

struct PersonAndProgram<'a> {
    #[builder(setter(into, strip_option))]
    my_name: (String, String, Option<String>),
    your_name: (String, String, Option<String>),
}
impl Datas {
    fn from_default_languages(&mut self) -> HashMap<String, String> {
        let mut data_lang = HashMap::new();
        data_lang.insert("عرب(Arabian)".to_string(), String::from("السلام عليكم"));
        data_lang.insert("English".to_string(), String::from("Hello"));
        data_lang.insert("हिंदी(Hindi)".to_string(), String::from("नमस्ते"));
        data_lang.insert("日本人(Japanese)".to_string(), String::from("こんにちは"));
        data_lang.insert("한국어(Korean)".to_string(), String::from("안녕하세요"));
        data_lang.insert("中文(Chinese)".to_string(), String::from("你好"));
        data_lang.insert("Português".to_string(), String::from("Olá"));
        data_lang.insert("Русский".to_string(), String::from("Здравствуйте"));
        data_lang.insert("Español".to_string(), String::from("Hola"));
        // let mut some_inter = BTreeMap::<String, String>::new();
        self.languages = data_lang;
    }
}

In the future I'd suggest you apply rustfmt to your code (perhaps using play.rust-lang.org) before posting it here, since you're more likely to get helpful advice that way.

2 Likes

So you don't know something to the questions above, at least several of them?)

The basic way to declare a list of a type like String is to use a vector:

struct Strings {
    strings: Vec<String>
}
1 Like

Yes, but if I want something like in macros for *,+... Etc)

Could you give an example of the kind of macro you're thinking of, or of what you're trying to achieve with your arbitrary number of Strings?

    • First, it looks like you're using the derive_builder crate. In that case you have to put the #[derive(Builder)] attribute on your struct Person_and_program to use the #[builder(...)] attribute on the fields.
    • Your struct Person_and_program is generic over a lifetime parameter 'a, but it does not contain any references. This will not compile: you should remove the <'a>.
    • I don't know of any way to apply an attribute like #[builder(...)] to multiple fields of a struct at once. I would recommend you not worry about cluttering up the code and just repeat the attribute wherever you need it.
  1. Unfortunately I don't really understand what you're asking about here. Using JSON to store and transfer data is common and straightforward in Rust: check out the serde_json crate and its documentation, which will have lots of details. It sounds like you might be trying to do internationalization (translating text in your program into various languages) -- if you look at the corresponding category on crates.io you may find libraries that will help. Some miscellaneous notes on your code here:

    • The name and type signature of from_default_languages don't make sense: according to the type signature the method returns HashMap<String, String>, but in the body you're assigning data_lang to self.languages instead of returning it. Again, this will not compile. Also the "from" in the method name suggests that this is supposed to be a constructor, but in that case it should have a signature like fn from_default_languages() -> Self, because in a constructor there's no &mut self to work with.
    • For string literals, .to_string() and String::from do the same thing. You should pick one and use it consistently.
  2. If you aren't comfortable using a database, you can serialize your data to a format of your choice using serde and save that to a file, then deserialize back into a Rust type later.

  3. Check out these crates. If you describe what you want to do in more detail we might be able to suggest something more specifically.

Finally, I would also really suggest reading The Rust Programming Language if you haven't already, since it seems like you might be missing some Rust basics. You're welcome to post any questions you have on this forum (but format your code first please!).

2 Likes

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.