Hacker News new | ask | show | jobs
by ranguna 846 days ago
In rust I have a file that defines a struct and its implementations, then I have another file that has a static array of elements of that struct. But in the struct file, one of the implementations is a TryFrom<usize>, (which is ran when you have a variable of type usize and "cast" it into my struct) this TryFrom implementation returns the value of the nth element of the static array in the second file. I don't see anything wrong in having this circular dependancy.

Potentially I could extract the TryFrom implementation into a 3rd file, breaking the circle, but tbh that feels like I'm doing that just for the sake for doing that, and it offers no real benefit.

In this case I see a benefit in keeping the struct and its implementation in a file, and another file with a static variable (which btw is around 600 lines, yeah it's a big array) in a separate file.

Don't follow rules blindly and try not to have absolute rules in your life, it'll make things simpler and more flexible.

1 comments

Circular dependencies between files are fine, subject to language limitations.

Circular dependencies between directories are what's usually an indicator of something wrong.