rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.88k stars 1.56k forks source link

add "#region" like C# #2062

Closed 3442853561 closed 7 years ago

3442853561 commented 7 years ago

add "#region" like C#

golddranks commented 7 years ago

You should explain in the first post what #region does, since not all know C#.

If I recall correctly, it's just a directive for the IDE that allows defining a "logical" region of source code, which the editor can, for example, collapse. It doesn't affect the actual code semantics in any way.

It should be noted also, that Rust allows already attributes using syntax #[attribute]. Maybe using the already existing attribute syntax for IDE region hints could be feasible?

eddyb commented 7 years ago

You can just put it in comments as it done for C, C++, etc.

wesleywiser commented 7 years ago

As a C# developer, the only time I've seen #region used properly was to allow you to collapse the licensing text at the top of a source file. The other 99% of the times it has been to hide the fact that the file is way too long. If your file/class/function is big enough that you need to use regions, it's past time to break things up.

jaroslaw-weber commented 7 years ago

lot of c# programmers discourage use of region, you can do collapsing with ide or you can split the code in files

ghost commented 6 years ago

I think this should be re-opened. There are uses for #region that aren't to "hide the fact that the file is way too long". Using #region for splitting up Public, Protected & Private members and methods helps to keep files tidy irrespective of whther they are long or short files.

anonymoustafa commented 4 years ago

As a C# developer, the only time I've seen #region used properly was to allow you to collapse the licensing text at the top of a source file. The other 99% of the times it has been to hide the fact that the file is way too long. If your file/class/function is big enough that you need to use regions, it's past time to break things up.

Dear Wesley Wiser,

  1. Rust-lang is a functional language, we can't break things up without elongating our code!
  2. Using an attribute like #region in Csharp helps to the legibility of the code.
forteller11 commented 2 years ago

As a C# developer, the only time I've seen #region used properly was to allow you to collapse the licensing text at the top of a source file. The other 99% of the times it has been to hide the fact that the file is way too long. If your file/class/function is big enough that you need to use regions, it's past time to break things up.

I don't think the C#/Java OOP tendency to separate everything every little chunk of state+function into it's own file is necessarily a good habit. Especially for a systems programming language long files can be expected. Systems do not get less complex just because their complexity is amortized over the filesystem. #region #endregion are a way to program in this systems programming style while still offerings progs softer methods to group and compartmentalize features within a single file.

truppelito commented 2 years ago

Personally I'm a big fan of the way Swift/Xcode handle this. Swift uses comments with specific keywords such as: // MARK: xxx, // MARK: -, // TODO: xxx, // FIXME: xxx, etc. In my opinion, this is in line with Rust, as we already have the /// xxx format for documentation. Also, it would not require any changes to the language itself: this is just an addition to the common practices of the language. Finally, it would easily be picked up by any IDE (example of how this works for Swift in Xcode).

Edit: I would also make the argument that comments are better suited for non-code/documentation/organizational information than #regionor [#attribute] or other more "code-like" constructs (at least, this is my preference).

eddyb commented 2 years ago

Swift uses comments with specific keywords such as: // MARK: xxx, // MARK: -, // TODO: xxx, // FIXME: xxx, etc.

I've seen (and used) the TODO/FIXME ones (or, my favorite, HACK) in most languages I've used.

// FIXME: ... (and the "attributed" variant, e.g.: // FIXME(eddyb) ...) comments are widely used in the Rust compiler itself, and have been for the past decade.

And some IDEs will highlight some of those comments across multiple languages.


However, I haven't seen anything that common for collapsible regions.

But a quick search for VSCode reveals https://marketplace.visualstudio.com/items?itemName=maptz.regionfolder which after some quick testing does seem to work with /* #region ... */ /* #endregion */ comments in Rust (sadly it doesn't support // comments for Rust but it does for C, so it can likely be fixed to treat Rust like C, or the instructions in its documentation can be used to locally add support).


So IMO, there are two separate issues here:

OTOH, I would not expect non-comment alternatives to be a viable proposal (that is, one could try to open an RFC for them, but I doubt it would receive a lot of support).

My best guess as to how this could move forward, would be someone posting to the internals and/or users forums to gather some opinions on what people might want to use (i.e. subjective aesthetics).

If this does lead to some kind of agreement, there are a few places where I've seen/added a non-doc comment (with empty lines before and after it), to indicate the start of a new "section", that could use it.