Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

A File And Directory Structure For Rust Projects

**NOTE:** This post is an early draft. The source code for the files and specific notes are on the way. For now, the files are on GitHub at:

[[https://github.com/alanwsmith/rust_directory_structure][alanwsmith/rust_directory_structure]]

_DRAFT_

Rust has my attention. I spent the past two weeks beginning my journey with the language. It gets a lot of love and I see why.

Several aspects of Rust are new to me. Some are not. For example, I don't want to throw everything in a single file. I want functions split out in a directory structure that makes sense with a strong emphasis on testing. Something I never really found with Python.

I mean, I had a standard Python setup, but it always felt brittle and hacked together. Code worked, but I was never confident I was doing it The Right Way(tm). I wanted to see if I could get more confidence as I start with Rust.

There are some posts on directory and file structures for Rust projects scattered around. None of the ones I found cover all the things I'm after. So, I spent the past couple of days figuring out a setup myself.

(At this point I'll throw in the reminder that while I've got a good bit of experience coding in other language I'm only a couple weeks in on Rust. I've got tests integrated and passing so I expect things are safe but I may be way off the mark in terms of doing things in a rustacean way. If that's the case and you know better I welcome any feedback.)

This is the checklist I setup as a requirements list:

- [x] Single `main.rs` and `lib.rs` files in the root `src` directory - [x] A `src/structs` directory with individual directories for each struct - [x] A single `use rust_structure::functions::*;` in `main.rs` to get all the structs - [x] A single `use rust_structure::structs::*;` in `main.rs` to get all the structs - [x] Default values for fields in structs - [x] Setup structs to work with `dbg!` - [x] A `methods` directory for each struct - [x] A single file for each struct method - [x] The ability for the methods in each individual file to talk to each other - [x] Unit tests written directly in the method files - [x] The ability for methods to talk to each other when testing (just to confirm) - [x] A consistent name for struct source files (e.g. `src/structs/alfa_struct/source.rs` so the name of the struct isn't duplicated in the file structure (e.g. `src/structs/alfa_struct/alfa_struct.rs`) - [x] A separate directory for struct associated functions - [x] Unit tests build directly into associated function files - [x] A single `src/functions` directory for functions that aren't associated with structs - [x] A directory for functions for `main.rs` to use directly instead of through a struct. - [x] Unit tests directly inside top level functions - [x] Ability for top level functions to talk to themselves - [x] Individual files for associated functions - [x] Test for associate functions inside the files they are testing

NOTES:

- I'm keeping the "Default" associated function in the same file as the struct since updating one requires changing the other.

TODO:

- Make a `tests` directory next to `src` and maybe a `resources`? live you've seen in some other repos - Figure out if naming directories `structs` or `functions` can cause issues