Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

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]]


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 [TODO: Code shorthand span ] and [TODO: Code shorthand span ] files in the root [TODO: Code shorthand span ] directory - [x] A [TODO: Code shorthand span ] directory with individual directories for each struct - [x] A single [TODO: Code shorthand span ] in [TODO: Code shorthand span ] to get all the structs - [x] A single [TODO: Code shorthand span ] in [TODO: Code shorthand span ] to get all the structs - [x] Default values for fields in structs - [x] Setup structs to work with ` dbg! ` - [x] A [TODO: Code shorthand span ] 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 [TODO: Code shorthand span ] directory for functions that aren't associated with structs - [x] A directory for functions for [TODO: Code shorthand span ] 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


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


- Make a [TODO: Code shorthand span ] directory next to [TODO: Code shorthand span ] and maybe a ` resources ` ? live you've seen in some other repos - Figure out if naming directories [TODO: Code shorthand span ] or [TODO: Code shorthand span ] can cause issues