15
submitted 1 year ago* (last edited 1 year ago) by boblaw0@lemmy.world to c/rust@programming.dev
all 9 comments
sorted by: hot top controversial new old
[-] PoolloverNathan@programming.dev 10 points 1 year ago

Whenever I need a struct, I flip a coin to decide if it should be above or below the one I'm currently writing.

[-] snaggen@programming.dev 6 points 1 year ago

Order? I write them as I need them... is my code chaos, yes!

[-] KillTheMule@programming.dev 5 points 1 year ago

Enums/Structs first, but those 2 are mixed, and any impl for them will be directly after the definition of the type itsef. Free functions last.

[-] sirdorius@programming.dev 4 points 1 year ago* (last edited 1 year ago)

I like to write them in blocks of concerns: ie there should be the minimum amount of jumping around the file when reading them. If there are a lot of these separate blocks in a single file, that is a good indication that it has grown enough and it is time to separate them into different files. But inside the blocks it would be usually struct/enums (no implicit order since they are just sum/product types), impl blocks, free functions (that are usually common functionality between the impl blocks).

[-] hairyballs@programming.dev 3 points 1 year ago* (last edited 1 year ago)
  • I put the types first in the file, sorted by importance
  • then the public free functions
  • then the impl blocks, sorted by importance, also. Usually, display impls and similar end up being at the end
  • then the private free functions (helpers)

The idea is that I can see all the types in one glance, then I look at the rest.

[-] nous@programming.dev 3 points 1 year ago* (last edited 1 year ago)

I don't have any hard rules for things beyond whatever makes things easier to read. And I will move things around to get to the goal as I see fit (sometimes this can change over time - especially if you come back to something after a few months).

Generally speaking I would start with more global types at the top, though I might have some types next to the functions when they are only used by the function. And for functions I would start with main followed by functions main uses in rough order that they appear. With functions used by other functions below that functions - sometimes below others if I care more about the flow of higher functions more. Whatever I find makes it easier to read from top to bottom basically. I do the same with types, define the main ones first, then followed by types they use or types you will want to be aware of next. I don't care what the type is - I don't group enums together - they are all just types to me and it is often better to mix up their order based on how important they are rather than what type they technically are.

I really dislike how so many languages force you to do the reverse of this and declare things before you use them. IMO that is like reading a book starting at the last chapter and means you often have to skip over loads of things to figure out where the start is. Much better to have the most important part at the top of the file.

[-] ohlaph@lemmy.world 3 points 1 year ago

I place them in that order, enums, structs, functions.

[-] fil@hachyderm.io 2 points 1 year ago

@boblaw0 In an order that tells the story, when reading the module file top-to-bottom. It is usually difficult to do when rapidly developing, but easier to achieve when refactoring a working program or a library.

this post was submitted on 06 Sep 2023
15 points (100.0% liked)

Rust

5980 readers
79 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS