5 ms·
I admit, I don't know much about Rust. However, I know C/C++ a bit and this drives me nuts: int* foo; // This is a pointer. Understood. int *foo; // How is
by fermienrico 9y ago
I admit, I don't know much about Rust. However, I know C/C++ a bit and this drives me nuts:
int* foo; // This is a pointer. Understood.
int *foo; // How is this allowed!???? Confusing!
*foo = bar; // Now you're deferencing. WTF!
In the second line, int foo confuses the hell out of me when I am scanning through pages of code. All of this mess could be solved by doing the pointer symbol after the variable name:
int foo*; // This is a pointer
- qznc 9y agoThe philosophy in C/C++ is "you declare it how you use it". You declare it with `foo`, you use (assign to) it as `foo`. Likewise for arrays. You declare it `int foo[]` and you use it like `foo[i]`.
- lokedhs 9y agoBecause the * belongs to the variable, not the type. It's more clear with this example, which declares two variables at the same time: int *foo, bar; This declares foo as a pointer to int, and bar as an int. The following, written in the style you suggest, do the same thing but can be confusing, since a casual reader might think that this declares both foo and bar to be pointers to int: int* foo, bar; Someone else commented that C's declarations are designed to look the same as how they are used. This is true, and explains why arrays are declared the way they are, where the brackets come after the variable name: int foo[10]