5 ms·
What are you talking about? Rust’s function signature and type declaration syntaxes are extremely vanilla, unless you venture into some really extreme use cases
by simonask 11mo ago
What are you talking about? Rust’s function signature and type declaration syntaxes are extremely vanilla, unless you venture into some really extreme use cases with lots of lifetime annotations and generic bounds.
I seriously don’t get it.
fn add(a: i32, b: i32) -> i32 { … }
Where’s the “Perl-esqueness”?
- simonw 11mo agotrait Handler { fn handle<'a>(&self, input: &'a str) -> Result<&'a str, HandlerError>; } fn process_handler<'a>( handler: Box<dyn Handler + 'a>, input: &'a str, ) -> Result<&'a str, HandlerError> { handler.handle(input) }
- simonask 11mo ago> > really extreme use cases with lots of lifetime annotations and generic bounds You choose as your example a pretty advanced use case.
- nullpoint420 11mo agoWhich is the exact use case someone would choose rust for over other languages
- creata 11mo agoNo, the use cases of Rust are pretty much the same as the use cases of C++. Most Rust code shouldn't have objects with complicated lifetimes, just like most code in any language should avoid objects with complicated lifetimes.
- simonw 11mo agoYeah, because if you exclude the bits that make Rust look like Perl then it won't look like Perl!
- tcfhgj 11mo agonow show me an alternative syntax encoding the same information
- bgwalter 11mo ago...
- steveklabnik 11mo agoThere's a deeper connection there: lifetimes are a form of type variable, just like in OCaml.
- deleted 11mo ago[deleted]
- deleted 11mo ago[deleted]
- piva00 11mo agoCould have thrown a few uses of macros with the # and ! which threw me off completely while trying to read a Rust codebase as a non-Rust programmer.
- WD-42 11mo agoLifetimes really only come into play if you are doing something really obscure. Often times when I’m about to add lifetimes to my code I re-think it and realize there is a better way to architect it that doesn’t involve them at all. They are a warning sign.
- quux0r 11mo agoWhile I don’t disagree that this is at first blush quite complex, using it as an example also obscures a few additional details that aren’t present in something like python, namely monads and lifetimes. I think in absence of these, this code is a bit easier to read. However, if you had prior exposure to these concepts, I think that this is more approachable. I guess what I’m getting at here is that rust doesn’t seem to be syntactic spaghetti as much as it is a confluence of several lesser-used concepts not typically used in other “simpler” languages.
- creata 11mo agoThat's just a weird and unrealistic example, though. Like, why is process_handler taking an owned, boxed reference to something it only needs shared access to? Why is there an unnecessary 'a bound on handler? In the places where you need to add lifetime annotations, it's certainly useful to be able to see them in the types, rather than relegate them to the documentation like in C++; cf. all the places where C++'s STL has to mention iterator and reference invalidation.
- steveklabnik 11mo agoI agree that the signature for process_handler is weird, but you could steelman it to take a borrowed trait object instead, which would have an extra sigil. The handler function isn't actually unnecessary, or at least, it isn't superfluous: by default, the signature would include 'a on self as well, and that's probably not what you actually want. I do think that the example basically boils down to the lifetime syntax though, and yes, while it's a bit odd at first, every other thing that was tried was worse.
- creata 11mo ago> The handler function isn't actually unnecessary, or at least, it isn't superfluous: by default, the signature would include 'a on self as well, and that's probably not what you actually want. To clarify, I meant the 'a in `Box<dyn Handler + 'a>` in the definition of `process_handler` is unnecessary. I'm not saying that the <'a> parameter in the definition of Handler::handle is unnecessary, which seems to be what you think I said, unless I misunderstood.
- steveklabnik 11mo agoAh yes, I misunderstood you in exactly that way, my apologies.
- WD-42 11mo agoLLMs LOVE to write Rust like this. They add smart pointers, options and lifetimes everywhere when none of those things are necessary. I don’t know what it is, but they love over-engineering it.
- haolez 11mo agoThat's simple even in Perl. The problem is when you start adding the expected idioms for real world problems.