Dynamic struct creation,

Hey guys,

I am new to Rust and have been working through the book. I am simultaneously working on a small project so that I can try things out on my own.

My project requires that I create new users and I want to store the user info in a text file. My trouble is as follows:

  1. To create a struct that dynamically takes user input. All tutorials always use static data but I want to build the struct from user input via console.Ideally, I want to create a function that takes user input and appends it to the struct in one go.

  2. To write an entire struct or tuple to a text file. At this point I can write one of the data fields of the struct to the file, but not all at once.

Any guidance will be appreciated.


It'd be nice if you'd paste the code in directly and format it.
Nevertheless, on to your actual questions.

  1. Structs don't take user input. They serve exactly one purpose - to store data. So, what I think you want to do is simply to store all the fields in a struct. Judging by your code, you have succeeded in doing so. So not entirely sure what your question is, some elaboration would help.
  2. From what you've mentioned, I think you want to do something like write!(&mut file, "{}", data).unwrap() and write the data in one go. The proper way to do that would be to implement the Display trait and then you'd be able to use the syntax I mentioned (traits and how they are used is in a later chapter in the book, so you'd have to read on).
    Alternatively, you can write a function of the form - fn write_data(user: &Data, file: &mut File) or preferably using an impl block - fn write(&self, file: &mut File). Then write the data out field by field.
    There is yet another more advanced technique, which is actually the one you'd use once you're used to Rust. Writing a struct to a file is known as "serialization". There are third party (and very popular) Rust crates to do this. They use something known as "procedural macros" (a fairly advanced concept) to automagically detect the struct fields and write them out to a file in some syntax (JSON, TOML, YAML, CSV, ...)
2 Likes

Thanks for the prompt response, next time I'll paste the code.

I just figured it the code would be more readable if I could do both user input and storing to the struct in one go.

I'll read on till I get to traits and have another go.

Well, that is called deserialization, the opposite of serialization. The crate I was talking about is called serde, and contains traits to define your own serialization format and proc-macros to automatically create functions which can convert your struct to and from that format. Then you can do the input and storing in one go.
I would advice reading upto traits at least before using this.

2 Likes

Has anyone done literal user input for serde? Eg prompt per field, or the like? That seems like a cool use.

I finally came to the realization that I had a fundamental misconception of Data Structures!

I only now realize that the purpose of data structures is for in-memory use, whereas a storing into a CSV file or DB would be for long term storage.

From a theoretical perspective, the things we put in files are also data structures. It's just that the kind of data structures that are good for memory (efficient to access repeatedly; thrown away when the program exits) are often not that good for disk (should be space-efficient; compatible with different programs or future versions). As a consequence, they are designed in very different ways, and talked about with different concepts. But there's a common foundation of "we have a sequence of bytes; how can we use that to represent useful information?”

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.