Hacker News new | ask | show | jobs
by nick__m 664 days ago
I am not a rustefarian so take my opinion on that for what it's worth: i.e. not much. [I tried the language about 5 years ago and I was disturbed by the dbus crate overloading of the dereference operator, to Rust practitioners I ask is &*msg.interface() an axiomatic Rust construct ?]

From what I understand from the article, returning a dyn Error in a Result appears to be about as useful as throwing a new Exception("something bad happened") in java. I am sure it gets better in part two "Structured Rust errors" but this guide on error handling doesn't increase my motivation to give Rust another chance.

2 comments

Yes, `&*foo` is meaningful. For example, `Vec<T>` becomes `[T]` and then `&[T]`.

Yes, a `Box<dyn Error>` is about the most rudimentary way of returning an arbitrary error, but it’s better than a panic or a sentinel value.

Your example is not what the dbus crate does, in your case everything is related to T, it doesn't surprise!

In that crate the &*msg.interface().unwrap() magically become an &str !

I don’t know the dbus crate, so I’m not trying to say anything about that crate’s usage, I’m simply saying that referencing after dereferencing is idiomatic Rust.

For example, the standard library `String` type dereferences to `str` which you can then re-reference to `&str`.

Sorry, I should have been clearer in my question, I wanted to know about A deref to B re-ref to &B.

ameliaquining awnsered my question and you modulated my opinion, that pattern is can be idiomatic when the principle of minimal surprises is respected.

This is controversial, but I would say that the way the dbus crate uses Deref is not idiomatic Rust. https://rust-unofficial.github.io/patterns/anti_patterns/der...
Thank you for that link, it exactly describes my experience and the rational behind my opinion:

  The Deref trait is designed for the implementation of custom pointer types. The intention is that it will take a pointer-to-T to a T, not convert between different types. It is a shame that this isn’t (probably cannot be) enforced by the trait definition.

  Rust tries to strike a careful balance between explicit and implicit mechanisms, favouring explicit conversions between types. Automatic dereferencing in the dot operator is a case where the ergonomics strongly favour an implicit mechanism, but the intention is that this is limited to degrees of indirection, not conversion between arbitrary types.
Given that `Interface` is "A wrapper around a string that is guaranteed to be a valid D-Bus interface name", it's not surprising that derefering it would provide the underlying `String`. And then it's “deref-coerced” for you into &str. &str and &String are different types but it's not a conversion between arbitrary types. It's definitely not magic. Personally, I think it is reasonable and quite helpful. It would be a shame if this was the whole reason you gave up on Rust 5 years ago, I hope it wasn't!
That and the always changing crates required since the std lib was anemic and the stupidily long compilation time.

But I must admit that Cargo and clippy were nice and lifetime are easier to understand than when using std::move in c++ is required vs when it's implicit.

But really what made me abandon learning Rust is Nim. My first Nim program I made a small maze game in about 200 lines.

Also the fact that I now mostly program recreationally since I pivoted from software development to infrastructure ( my title is system analyst) at a research University (absolute job security is really nice). Software development at that institution was getting to political to my taste and I like to play with nice hardware!