Skip to content

Commit 051be99

Browse files
author
Hero Bird
authored
Update 2540-formal-function-parameter-attributes.md
1 parent 76da521 commit 051be99

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

text/2540-formal-function-parameter-attributes.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Having attributes on formal function parameters allows for certain different use
1616
## Example: Handling of unused parameter
1717

1818
In today's Rust it is possible to prefix the name of an identifier to silence the compiler about it being unused.
19-
With attributes in formal function parameter position we could have an attribute like `#[unused]` that explicitely states this for a given parameter.
19+
With attributes in formal function parameter position we could hypothetically have an attribute like `#[unused]` that explicitely states this for a given parameter.
20+
Note that `#[unused]` is not part of this proposal but merely a simple usecase.
2021

2122
```rust
2223
fn foo(#[unused] bar: u32) -> bool;
@@ -28,7 +29,7 @@ Instead of
2829
fn foo(_bar: u32) -> bool
2930
```
3031

31-
This would better reflect the explicit nature of Rust compared to the underscore prefix as of today.
32+
Especially Rust beginners might find the meaning of the above code snippet to be clearer.
3233

3334
## Example: Low-level code
3435

@@ -46,9 +47,36 @@ fn foo(
4647
Which might state that the pointers `in_a` and `in_b` might overlap but `out` is non overlapping.
4748
Please note that I am *not* proposing to actually add this to the language!
4849

49-
## Example: Procedural Macros
50+
## Example: Usable with cfg_attr
5051

51-
Also procedural macros could greatly benefit from having their own defined custom attributes on formal parameters.
52+
```rust
53+
fn foo(#[cfg_attr(foo, bar)] baz: u32);
54+
```
55+
56+
Which states that `baz` is attributed with the attribute `bar` if `foo` evaluates to `true`.
57+
58+
## Example: Documentation comments
59+
60+
Since documentation comments are attributes in their underlying representation we could have doc comments
61+
for single parameters like shown below.
62+
63+
```rust
64+
/// Description of foo.
65+
fn foo(
66+
/// Description of foo's first parameter bar
67+
bar: u32
68+
);
69+
```
70+
71+
Of course this raises other questions:
72+
- How does rust-fmt handle these?
73+
- Are they allowed or should we postpone their allowance to another RFC?
74+
75+
## Example: Procedural macros and custom attributes
76+
77+
Also procedural macros and custom attributes could greatly benefit from having their own defined custom attributes on formal parameters.
78+
79+
Examples will follow.
5280

5381
# Guide-level explanation
5482
[guide-level-explanation]: #guide-level-explanation

0 commit comments

Comments
 (0)