Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You changed your post, so this is a reply to the changes.

Boxes are containers, not pointers or references. Let's look at all of those those using the star syntax and operator inside of an unsafe block? You'll end up with stuff like:

    fn main() {
        let mut a = [0i32; 10];
        let p: *mut [i32] = &mut a;

        // Rust did NOT solve this problem
        let x: i32 = unsafe { (*p)[1] };

        print!("{x}\n");
    }
Note the annoying parentheses because they used prefix syntax for pointers, again.


> Boxes are containers, not pointers or references.

There is no actual difference between the two. Pointers and references could have been `Pointer<T>` and `Ref<T>` with the exact same ABI. They're not for UX reasons, one of similarity with C, and one of syntactic overhead.

> // Rust did NOT solve this problem

> let x: i32 = unsafe { (*p)[1] };

That is a debate of precedence, which is a very different concern than what I was replying about, and what TFA is about: the absurd mess that are C type declarations.


> There is no actual difference between the two.

Rust has raw pointers, references, slices, and arrays, each with special syntax in type/variable declarations. You're conflating those with Boxes, and it isn't adding clarity to the discussion. It's not like C++ removed this problem now that people use `std::unique_ptr<T>`.

Rust's raw pointer syntax is exactly as bad as C's, because it's the same syntax. Yes, it could use some other syntax like `Ptr<T>` for declaration, but it doesn't. And that still says nothing about what operator syntax to use when dererencing the pointer.

Perhaps it could be:

    let p: Ptr<T> = whatever;
    let x = p.deref(); // suffix syntax!
> That is a debate of precedence, which is a very different concern

It's not a different concern, and it's a problem no matter what the precedence. If prefix pointer syntax binds tightly, you'll need parens in one case. If it binds loosely, you'll need parens in another case:

    *(binds_tightly.foo[10]);  // subscript the array first because star binds tightly
    (*binds_loosely).bar[10];  // dereference the pointer first because star binds loosely
However, if pointer syntax is suffix (like arrays, function calls, and field accesses), you simply don't need parens in either case. The operations are just in order.

> (the absurd mess that are C type declarations)

Hating on C is trendy, that's fine. But a lot of people are confused about what the problem really is. It could be fixed with one change, but since people can't see it clearly, they change everything. And in cases like Go or Rust, they change almost everything and still don't solve the problem.

A language could achieve C's goal of "declaration follows usage" if pointers had suffix syntax. The prefix pointer syntax is where crazy "winding rules" and "Gibberish to English" translators come in.


“declaration follows usage” is not a worthy goal. It's unreadable and doesn't allow for creating new parametrized types.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: