Unlike c++, in #rust, function and struct declaration order does not matter.

What is the best practice then? From lower-level (no deps) to higher-level (all deps) like in c++? Or the reverse so that you start reading a file from the higher level code? #asknostr #devstr

Reply to this note

Please Login to reply.

Discussion

My preference would be struct declaration first, it makes much more sense when reading the code.

And between functions?

I wouldn't. I'm old school I guess, I prefer everything up front.

Structs all to front gives you an idea of what you're working with.

What about pub vs private structs /functions?

when having several structs I'd say it's mostly prettier moving them to their own files, because then you can use the filename to re-find your structs.

I do this. Structs above impls and functions.

New structs that come unto scope below existing functions but above functions that use them.

There's while folder and module structure you can consider too for organization.

Consistency across a code base is probably more important. Also, if C++ has a paradigm that is more organizational with little impact on code logic, probably fine to use it.

TIL c++ has ordering requirements. Makes me happy I started with Rust for systems stuff.

It's not that bad since it sets an expected order. I always start reading c++ files from the bottom up.

Good to know. I'll do this when I encounter c++ now

I prioritise readability which generally means higher level first

Absolute Chaos.

Found the JS dev 👆 🤣 jk

🌝

It actually doesn't matter in a lot more languages.

I was kidding tho. Kind of.

I am a weirdy, I guess. I prefer to do struct/enum first with method/functions/impls associated with the struct following, then add the next struct, etc.

lol, literally it doesn't matter

putting them in the same file is probably the most important thing, sometimes it is more logical to put the most dependent declaration first other times maybe better to show them in order

the compiler parses them all at once and sorts them in order of dependency so you are free to put the helpers before or after as you see fit

part of the reason why more modern languages do this is because it makes the initial pass on each source file faster, literally just split the file based on sentinel markers, usually a newline and some keyword, like in #golang it's func, var, const, import, type

i mean, should you define the types before the methods? usually the most useful thing to put at the top of a file is where the central point of documentation also will go, so you put struct and type definitions at the top and then readers can get the top down view of the package immediately

nostr:nevent1qvzqqqqqqypzp5x7h70mzt00s86r6lrfg2dm0pyp9tq7f5k48gszmd42cl4yk3nvqqstjvk47qrlpjke36vyy905ee0fa9saw6f046kt93dsfsfkcfpwhvq0tqwkc