Skip Navigation
“Truly Hygienic” Let Statements in Rust
  • Maybe a good idea for a post. But the amount of reaches required makes this icky.

    • Pretending people write:
      let Ok(x) = read_input() else { return Err(Error) };
      
      instead of
       let x = read_input().map_err(|_| ...)?;
      
    • Pretending people write:
       const x: &str = "...";
      
      instead of
       const X: &str = "...";
      
    • Pretending there exist people who have such knowledge of rust macros hygiene, ident namespaces, etc, but somehow don't know about how macro code expands (the "shock" about the compile error).

    Maybe there is a reason after all why almost no one (maybe no one, period) was ever in that situation.

  • [newbie] one-liner to avoid "temporary value dropped while borrowed"?
  • you’re not supposed to immediately reach for macros

    correct

    for small things you don’t quite like about the language.

    incorrect

    Excessive macro use makes it impossible for others (including your future self) to read your code

    N/A. the macro above is trivial.

    impossible for others to read your code and there’s often good reasons why it’s designed like it is.

    fiction

  • [newbie] one-liner to avoid "temporary value dropped while borrowed"?
  • There is a general mechanism in Rust that allows language users to add their own sugar. It's called macros 😉

    macro_rules! keep {
        (let $id:ident = $expr:expr => $($tt:tt)+) => {
            let $id = $expr;
            let $id = $id$($tt)+;
        }
    }
    
    fn main() {
        keep!{ let path = std::env::current_dir().unwrap() => .as_path() };
        println!("{path:?}");
    }
    

    You can remove let from the macro's fragment specifier and invocation.

  • Which code style to initialize structs?
  • Neither.

    • make new() give you a fully valid and usable struct value.
    • or use a builder (you can call it something else like Partial/Incomplete/whatever) struct so you can't accidentally do anything without a fully initialized value.

    Maybe you should also use substructs that hold some of the info.

  • Confused about linux as always
  • I will let you on a little secret.

    The best "support" you can get is support from upstreams directly (I'm involved in both sides of that equation). But upstreams will often only "support" you when you 1. run the latest stable version 2. the upstream source code wasn't patched willy-nilly by the packager (your distro).

    So the best desktop linux experience comes with using rolling distro that gives you such packages, with Arch being the most prominent example.

    The acquired knowledge that argues stability and tells you otherwise is a meme.

  • [BLOG] Why Rust mutexes look like they do - Cliffle
  • a better solution would be to add a method called something like ulock that does a combined lock and unwrap.

    That's exactly what's done above using an extension trait! You can mutex_val.ulock() with it!

    Now that I think about it, I don’t like how unwrap can signal either “I know this can’t fail”, “the possible error states are too rare to care about” or “I can’t be bothered with real error handing right now”.

    That's why you're told (clippy does that i think) to use expect instead, so you can signal "whatever string" you want to signal precisely.

  • The empire of C++ strikes back with Safe C++ blueprint
    • C++ offers no guaranteed memory safety.
    • A fictional safe C++ that would inevitably break backwards compatibility might as well be called Noel++, because it's not the same language anymore.
    • If that proposal ever gets implemented (it won't), neither the promise of guaranteed memory safety will hold up, nor any big C++ project will adopt it. Big projects don't adopt the (rollingly defined) so-called modern C++ already, and that is something that is a part of the language proper, standardized, and available via multiple implementations.

    would you argue that it’s impossible to write a"hello, world" program in C++

    bent as expected


    This proposal is just a part of a damage control campaign. No (supposedly doable) implementation will ever see the light of day. Ping me when this is proven wrong.

  • The empire of C++ strikes back with Safe C++ blueprint
  • The only (arguably*) baseless claim in that quote is this part:

    it’s theoretically possible to write memory-safe C++

    Maybe try to write more humbly and less fanatically, since you don't seem to be that knowledgable about anything (experienced in other threads too).

    * It's "theoretically possible" to write memory-safe assembly if we bend contextual meanings enough.

  • [BLOG] Why Rust mutexes look like they do - Cliffle
  • if you're really that bothered..

    use std::sync::{Mutex, MutexGuard};
    
    trait ULock<'a> {
        type Guard;
        fn ulock(&'a self) -> Self::Guard;
    }
    
    impl<'a, T: 'a> ULock<'a> for Mutex<T> {
        type Guard = MutexGuard<'a, T>;
        fn ulock(&'a self) -> Self::Guard {
          self.lock().unwrap()
        }
    }
    

    or use a wrapper struct, if you really really want the method to be called exactly lock.

  • [BLOG] Why Rust mutexes look like they do - Cliffle
  • If lock-ergonomics is as relevant to you as indexing, you're doing it wrong.

    I would rather take indexing returning Results than the other way around.

    One can always wrap any code in {||{ //.. }}() and use question marks liberally anyway (I call them stable try blocks 😉).

  • Nagle's algorithm - Wikipedia
  • I specifically mentioned HTTP/2 because it should have been easy for everyone to both test and find the relevant info.

    But anyway, here is a short explanation, and the curl-library thread where the issue was first encountered.

    You should also find plenty of blog posts where "unexplainable delay"/"unexplainable slowness"/"something is stuck" is in the premise, and then after a lot of story development and "suspense", the big reveal comes that it was Nagle's fault.

    As with many things TCP. A technique that may have been useful once, ends up proving to be counterproductive when used with modern protocols, workflows, and networks.

  • Nagle's algorithm - Wikipedia
  • I already mentioned why! It's common pitfall. For example, try a large HTTP/2 transfer over a socket where TCP_NODELAY is not set (or rather, explicitly unset), and see how the transfer rate would be limited because of it.

  • cushy v0.3.0 Released
    github.com Release v0.3.0 · khonsulabs/cushy

    Breaking Changes This crate's MSRV is now 1.74.1, required by updating wgpu. wgpu has been updated to 0.20. winit has been updated to 0.30. All context types no longer accept a 'window life...

    Release v0.3.0 · khonsulabs/cushy
    1
    slint 1.6.0 Released
    github.com Release 1.6.0 · slint-ui/slint

    Release announcement with the highlights: https://slint.dev/blog/slint-1.6-released Detailed list of changes: ChangeLog

    Release 1.6.0 · slint-ui/slint
    2
    InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)BB
    BB_C @programming.dev
    Posts 3
    Comments 213