favicon here hometagsblogmicrobio cvtech cvgpg keys

Cow is weird

#rust

uncomfyhalomacro | 2024-06-24 | reading time: ~1min

So I just found out something weird while learning and writing a tutorial for Rust,

    let body: &std::borrow::Cow<'_, str> = http_req.body();
    if buf_str.contains(&**body) {
        println!("Body is same in buf");
    }

So I found out that &**body is possible because

body() -> &Cow -> deref -> Cow -> deref -> str (Size cannot be known at compile time)

therefore, add a reference -> & -> &str

OR

we can just use .deref and since Cow already has Deref trait, it's actually a "double" deref like the previous explanation. However, there is a slight difference

OR

we can also just use .as_ref since Cow has Deref trait and the .as_ref method will reference the inferred type T self which is String but since it's a ref so it's &String which is then coerced to &str because of String's Deref trait. See https://doc.rust-lang.org/stable/src/alloc/string.rs.html#2683 and https://doc.rust-lang.org/stable/src/alloc/string.rs.html#2479

Articles from blogs I follow around the net

Some Thoughts on the Twitter Mass Exodus

Another wave of Twitter users are jettisoning the social media website in favor of alternatives. Some are landing in the Fediverse (Mastodon and other ActivityPub-enabled software). Others are going to BlueSky. Some are just outright abandoning social medi…

via Dhole MomentsNovember 19, 2024

hyper in curl Needs a Champion

tl;dr - hyper in curl is nearly complete, but it needs a champion. Without a partner actively engaged that wants to enable and ship, it’s now on the path for being deprecated and removed. It needs a champion, a backing vendor or distro. Will that be you? …

via seanmonstarNovember 19, 2024

Swift observations from a reluctant Rustacean

Recently I've been thinking about Swift in terms of Rust, & have appreciated anew some of the choices made. In Rust. There's been a proliferation of X vs Y posts on the web, especially since the advent of LLM AI, so I try to resist the format. It's often e…

via Mike KreuzerNovember 16, 2024

Go Concurrency Patterns

Goroutines Channels Select Statement Wait Groups Mutex Conditions Atomic Operations Once Context Map Real Examples Goroutines The go keyword is used to start a goroutine. A goroutine is a lightweight, managed thread used by the Go runtime to run functions …

via Posts on integralistNovember 15, 2024

anarchism starts in the now: hope for a better future

there is still time

via maia blogNovember 14, 2024

Why I Will Always Be Angry About Software Engineering

Why do I bother getting angry about software? When I started writing, it came from a place of ennui — absolute despair at the amount of waste I was seeing in the technology sector since leaving university. I was paid spectacularly well, but nothing I produ…

via LudicityNovember 12, 2024

Generated by openring-rs

favicon here hometagsblogmicrobio cvtech cvgpg keys