Best Practices for Durable Storage in Rust

Hello All,

I'm making a ToDo Application that has just reached the stage where we need durable storage to continue developing features (the list of tasks should persist through each running of the .exe).

  • (side note: i do love databases, but i find plaintext formats better for interoperability and reducing complexity by avoiding a full-blow Postgres Implementation).

My original Idea was to do a 2-step cloning process to create a CSV file that the program reads and writes to on start and stop:

  1. when the program starts, it reads the proper CSV, and fills the task list with any existing task records.
  2. on close, it truncates that same file, and places the records it currently holds into the contents of the file.

Is this a good method for durable storage in rust? or is there a language feature that I could use for durable storage instead?

Non-functional: the app is currently only focused on a single person and his/her/their tasks, so synchronizing between diverse sources isn't an issue for now.

As always, I appreciate everyone's expertise and patience with a new rust programmer.

Bes Regards.

There are no special language features for durable storage. That's entirely a library thing.

If you want good libraries for it, I would recommend looking into these two:

  • serde: Rust's serialize/deserialize library. If everything fits into memory, it's probably the easiest way to turn a file into a data structure and back again. It can be used alongside the standard library file interface using the serde_json from_reader and to_writer functions to perform the actual I/O efficiently.

  • sqlite bindings rusqlite: If it doesn't all fit into memory, then you're going to start hitting Greencodd's Tenth Rule Of Programming. I understand why you don't want the operational complexity of an RDBMS server like postgresql. But sqlite is just a library, not a server, so it doesn't have all that hassle. It's also one of the most thoroughly-tested software projects in the world.

6 Likes

If you want interoperable storage but don't need performance, sqlite is perfect (single file, light libraries exist in every language).

More like CSV, but with strong standards, and interoperable, you have (by order of increasing compactness) XML, JSON, UBJSON, CBOR.

If your data is more like config, there is TOML, YAML, INI, etc.

If you finally don't need interoperability, there is sled for local database, or bincode (very compact and fast equivalent of JSON, it can represent almost any Rust struct losslessly using serde).

2 Likes

Thanks,

For now, i don't think that this data will have a problem residing in any hard disk since it's only a csv file with text data, but i suspect as the project's complexity grows, this will be one of our first maturation steps.

Thanks for the balanced review

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.