News for hackers. Stuff that matters.
Re:Apt Rust requirement raises questions by simonw on Tuesday November 25, 2025 @10:31AM (#46046719)

  trait 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)
  }
Re:Apt Rust requirement raises questions by creata on Tuesday November 25, 2025 @11:18AM (#46047312)
That'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.

Re:Apt Rust requirement raises questions by WD-42 on Tuesday November 25, 2025 @11:32AM (#46047490)
LLMs 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.
Re:Apt Rust requirement raises questions by nrds on Tuesday November 25, 2025 @03:04PM (#46050058)
As a first guess, they're trained on lots of social media and Q&A content. The former has lots of complaints about "look how complex rust is!" while the latter has lots of "help I've written very complex rust".
Re:Apt Rust requirement raises questions by steveklabnik on Tuesday November 25, 2025 @11:31AM (#46047481)
I 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.

Re:Apt Rust requirement raises questions by creata on Tuesday November 25, 2025 @11:33AM (#46047510)
> 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.

Re:Apt Rust requirement raises questions by steveklabnik on Tuesday November 25, 2025 @11:46AM (#46047700)
Ah yes, I misunderstood you in exactly that way, my apologies.
Re:Apt Rust requirement raises questions by WD-42 on Tuesday November 25, 2025 @11:03AM (#46047117)
Lifetimes 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.
Re:Apt Rust requirement raises questions by tcfhgj on Tuesday November 25, 2025 @10:37AM (#46046790)
now show me an alternative syntax encoding the same information
Re:Apt Rust requirement raises questions by bgwalter on Tuesday November 25, 2025 @10:53AM (#46047003)
...
Re:Apt Rust requirement raises questions by steveklabnik on Tuesday November 25, 2025 @10:58AM (#46047066)
There's a deeper connection there: lifetimes are a form of type variable, just like in OCaml.
Re:Apt Rust requirement raises questions by quux0r on Tuesday November 25, 2025 @11:14AM (#46047244)
While 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.
Re:Apt Rust requirement raises questions by simonask on Tuesday November 25, 2025 @10:35AM (#46046769)
> > really extreme use cases with lots of lifetime annotations and generic bounds

You choose as your example a pretty advanced use case.

Re:Apt Rust requirement raises questions by nullpoint420 on Tuesday November 25, 2025 @11:09AM (#46047194)
Which is the exact use case someone would choose rust for over other languages
Re:Apt Rust requirement raises questions by creata on Tuesday November 25, 2025 @11:28AM (#46047442)
No, 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.
Re:Apt Rust requirement raises questions by simonw on Tuesday November 25, 2025 @03:29PM (#46050363)
Yeah, because if you exclude the bits that make Rust look like Perl then it won't look like Perl!
Re:Apt Rust requirement raises questions by piva00 on Tuesday November 25, 2025 @10:37AM (#46046797)
Could 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.