• Giooschi@lemmy.worldOP
    link
    fedilink
    English
    arrow-up
    0
    ·
    18 days ago

    I can agree that the example function is not the best usecase. But the point still stand that there’s no realistic escape hatch from lifetimes and memory management in Rust.

    Cow does not work when you are actually required to return a reference, e.g. if you’re working with some other crate that requires that. Cow also has some more strict requirements on reborrows (i.e. you can reborrow a &'short &'long T to a &'long T, but you can only reborrow a &'short Cow<'long, T> to a &'short T).

    LazyLock can solve very specific issues like static, but is not a general escape hatch. Again, the example is not the best to showcase this, but imagine if you have to perform this operation for an unknown amount of runtime values. LazyLock will only work for the very first one.

    • BB_C@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      18 days ago

      Cow does not work when you are actually required to return a reference

      What does that even mean? Can you provide a practical example?

      (I’m assuming you’re familiar with Deref and autoref/autoderef behaviors in Rust.)