1. 1

  2. 2

    Yeah, that’s an important tool to have in one’s toolbox! I ended up just writing this snippet of code

    pub fn timeit(label: impl Into<String>) -> impl Drop {
        let label = label.into();
        let now = std::time::Instant::now();
        defer(move || eprintln!("{}: {:.2?}", label, now.elapsed()))
    pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
        struct D<F: FnOnce()>(Option<F>);
        impl<F: FnOnce()> Drop for D<F> {
            fn drop(&mut self) {
                if let Some(f) = self.0.take() {


    which I just copy-paste verbatim every time I need this functionality in a random projects.

    Note: do use {:.2?} formatting in such cases, excessive precision is rather distracting when working on perf optimizations.

    1. 1

      Do you have an example of how you would call this? How do you inject the code to be measured?

      1. 2

        Here’s the edit I’d do to check which fraction of total auto-import functionality in rust-analyzer is due to searching for possible imports:


        Though, in case of rust-analyzer specifically, we actually have a dedicated module where we dump missing batteries, so I’d just use that:


        1. 1

          And then there was enlightenment!

          It was staring at me in the face, I over fixated on the Drop stuff to not realize that drop is used to signal the end of the block and emit the timing information. The docstring would be something like, “prints elapsed time to stderr when current scope drops”.

          Thank you, off to read stdx

      2. 1

        Why Option<F> instead of just F?

        1. 1

          For Drop. MaybeUninit<F> would’ve worked as well, but that needs unsafe.

          1. 1

            Ah, I didn’t realize you needed to own the function to call it.

            I incorrectly thought changing Option<F> to F would allow writing self.0().

      3. 1

        The didactic structure of the article is interesting in that takes one through the failure process in writing a seemingly simple Rust macro.