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

It's GPT-3.5 which has this horrible tendency to apologize for everything, up to a point, where it becomes a big annoyance.

GPT-4, on the other hand, no longer seems to have this problem. With GPT-4 I'm getting responses which exceed even what I'd expect from a human.

---

ME: Actually, the editor is telling me that it is an `Option<Arc<Vec ...`, it's just that it seems to be capable to do a `data.iter()` on the returned `Arc`, is this normal?

-

GPT: Yes, that is perfectly normal. When you have a smart pointer such as Arc<T> or Box<T>, you can call methods on T directly. Rust automatically dereferences the smart pointer for you.

In your case, Vec<T> has a method iter() which you can call directly on Arc<Vec<T>>. The Arc is automatically dereferenced, and the iter() method is called on the Vec<T> that it's pointing to.

This feature is called "Deref coercions" in Rust. It's a convenience feature which allows certain types of values to be automatically dereferenced as needed for function calls, method calls, and dot expressions. So even though data is of type Arc<Vec<T>>, you can call Vec<T> methods directly on data.

However, please note that data.iter() gives you an iterator over immutable references, not mutable ones. This is because Arc<T> only allows shared, immutable access to the data it manages. To get a mutable reference to the data, you have to use Arc::make_mut(), as I've explained in previous posts.

-

ME: Interesting. But it's a bit spooky that it does this automatically, because, per-se, an Arc should not contain those methods nor pretend to do.

-

GPT: Yes, the concept of dereferencing in Rust can indeed be a bit spooky at first glance, especially if you're coming from other programming languages. However, it's designed with strict rules that maintain Rust's guarantees of safety and concurrency.

The Deref trait in Rust is what allows such behavior. When a type T implements the Deref trait, it allows an instance of T to be treated like a reference of another type. So in this case, the Arc<T> struct implements the Deref trait, which returns a &T. This is why you're able to call methods of T directly on an Arc<T>.

However, Rust is also careful to not let this cause any safety issues. For instance, even though you can call Vec<T> methods on an Arc<Vec<T>> because of the Deref trait, Rust's borrowing rules still apply. So you can't mutate the Vec<T> through the Arc<T> unless you're the only one holding a reference to it (which is what Arc::make_mut() checks for).

In other words, Rust's Deref coercions can indeed feel a bit magic, but they are designed to work with Rust's other features to maintain safety.

---

The amount and relevance of the extra information provided is astonishing. "This feature is called "Deref coercions" ...", "The Deref trait in Rust is what allows such behavior. ..."



Wow




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

Search: