Login
You're viewing the ruby.social public feed.
  • Apr 9, 2026, 1:00 PM

    #Rust / #RustLang question: I have a type that, while an instance of it exists, provides some service (e.g. a web server). The Drop implementation of that type containts its teardown logic:

    fn main() {
    let s = Server::new();
    // … more code …
    spin_until_ctrl_c();
    // Teardown of s happens here implicitly
    }

    Now, rustc will of course complain about the unused instance. Same if I put it in a struct field or something, so the service is available as long as that object exists.

    1/2

    💬 2🔄 0⭐ 0

Replies

  • Apr 9, 2026, 1:00 PM

    What's the recommended way to deal with the warning and also communicate the intention ("don't drop/remove this object, because then the service will be unavailable") to anyone reading this code? I know I can prefix it with an underscore, but is there a better option? Like maybe a #[significant_drop_implementation] attribute or something?
    2/2

    💬 0🔄 0⭐ 0
  • Apr 9, 2026, 1:04 PM

    @guenther just rename the variable to `_s` this marks the data as intended to not be used.

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 1
  • Apr 9, 2026, 2:23 PM

    @michalfita @sebsch @guenther alternatively, add an explicit `drop(s)` after your ctrl-hook (and add a comment explaining why you are doing a manual drop. The actual drop call doesn’t “matter” but it makes the variable used and explicitly shows the reader that you expect it to live up to that point)

    💬 1🔄 0⭐ 0
  • Apr 9, 2026, 2:26 PM

    @ivan yeah, but is there something i can put on the type, e.g. if I'm providing the type in a library and want to give users a hint that it must be kept alive (and warn them if they do a let _ = on it)?

    @michalfita @sebsch

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Apr 9, 2026, 4:27 PM

    @guenther Would it be feasible to havesg have a method giving you a marker zero sized type on which the free function logic is called from or used as a token when calling it? It is good form to leverage the type system to ensure that two separate things that need to exist simultaneously actually are. For example, Mutex guards the accessible content behind new types, so you can't access the underlying value incorrectly.

    💬 1🔄 0⭐ 0
  • Apr 9, 2026, 4:54 PM

    @ekuber Sorry, I do not understand what your question means. Did you see the second part of my thread?

    💬 0🔄 0⭐ 0