home.social

#rusttips — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #rusttips, aggregated by home.social.

  1. Some #Rust string literal #trivia - the following are equal:

    let s = "one line string";

    let s = "one line \
    string";

    And these ones are also equal:

    let lines = "a\nmultiline\nstring";

    let lines = "a
    multiline
    string";

    and now my favorite... 🥁

    let lines = "a\n\
    multiline\n\
    string";

    Playground:
    play.rust-lang.org/?version=st

    #RustLang #RustTips #Strings

  2. 🎨 Rust Tip: Use cargo fmt to automatically format your code according to Rust style guidelines.
    📝 You can even setup your editor to Auto-Format on save!

    VS Code Instructions:

    - Install the Rust Analyzer extension
    - Open settings
    - Search for "Format on Save" and enable the setting

    Or you can add the following to your settings.json:

    Link to ref: users.rust-lang.org/t/how-to-a

    #RustTips #CodeStyle #Rust30by30 #Day12

  3. If you get weird #Rust #Lifetime #error's: have you already tried to just _remove_ the lifetime annotations and see what happens? You'll often get much better error messages this way. :ferris:

    If this doesn't work and you still can't figure it out: try to not use references and own your values instead.

    #RustLang #RustTip #RustTips #FerrisTipOfTheDay

  4. Did you know that a lot of things in #Rust directly implement the `Ord` trait?

    For example `Option<T>` where T: Ord

    doc.rust-lang.org/std/option/e

    So you can do:

    assert_eq(max(Some(0), Some(1)), Some(1))

    assert_eq(max(Some(0), None), Some(0))

    assert_eq(min(Some(0), None), None)

    There are a lot of other things that implement `Ord`:
    doc.rust-lang.org/std/cmp/trai

    #RustLang #RustTips #RustTip

  5. When reading arguments of when to use Associated Types (AT) vs. Generics (G) in #Rust, it's often (correctly!) argued that you should use AT over G, when an impl only makes sense for one single type (good example is the AT in `Iterator` or `Deref`).

    An additional benefit, that is often overlooked IMO, is that using AT is also _object safe_, so you can call methods that take AT as parameters on trait objects. ✨ :awesome:

    See this playground:
    play.rust-lang.org/?version=st

    #RustLang #RustTips

    1/2

  6. Oh wow, did you know that in #Rust you can use Option as an iterator such that your types align, when you want to return a chain of iterators from a function, but also have an else case where the iterator is empty!?

    I know, this was a mouthful.😳

    Let's look at a playground that shows this:
    play.rust-lang.org/?version=st

    This trick is right from the docs on option:

    doc.rust-lang.org/stable/std/o

    What a cunning trick!🤓

    Rust, I ❤️ you!

    #RustTip #RustTips #RustLang

  7. BinaryHeap in #Rust std is a max heap.

    If you want to have a min heap, you can use std::cmp::Reverse, to reverse the ordering.

    See this playground:

    play.rust-lang.org/?version=st

    #RustTips #RustLang

    Edit: just as I was tooting this, Jon has said it in the stream 😄

  8. 🦀 #RustTips

    A Crate to load environment variables from a .env file

    🙅 Don't use dotenv crate. It is unmaintained

    ✅ Use "dotenvy" crate which is fork of dotenv

    github.com/allan2/dotenvy

    rustsec.org/advisories/RUSTSEC

    #rustlang #rust #infosec #appsec

  9. 🦀 #RustQuiz

    What is the output of this?

    A. Error
    B. RustJsPython
    C. (Rust, Js, Python)
    D. Rust

    (Not hard/tricky or unique as #RustChallenge, just simple quizzes)

    #rustlang #rustquiz #rust #rusttips #LearnRust

  10. ✨ Rust Challenge explained

    Ans:
    2. A gives output and B gives error

    In `A` code:
    👉 x will be assigned to i8 type since we are using i8 to compare in assert_eq.

    👉 x.pow(6) will be 2 power 6 which is 64, it will work perfectly

    In `B` code:

    👉 x will be assigned default to i32

    👉 But in second line you will get ambiguity error 🤯

    👉 And compiler will ask to specify the type for x explicitly

    #rustlang #learnrust #rusttips #rust

  11. 🦀 Format String tricks - Minimum width

    👉 You can set minimum width for the characters that gets printed

    Eg:
    {var_name:min_width}

    👉 Helps to ensure that fields in the output are of a uniform width

    play.rust-lang.org/?version=st

    #rustlang #RustTips #rust #LearnRust

  12. ✨ Using variable inside Format String

    👉 It appears many people are not aware of this syntax that was introduced in Rust 1.58(2022/01/13)

    👉 You can also use format specifiers along with it

    #rustlang #rusttips #rust #learnrust #programming

  13. One more rust challenge code snippet for today:

    Can you solve this rust code snippet without running it? - You are the compiler

    (No cheating 😉)

    What will be the output?

    #rustlang #rust #programming
    #rusttips #rustchallenge #rustlanguage

  14. Can you solve this rust code snippet and find output without "running"? - You are the compiler

    (No cheating 😉)

    What will be the output?

    #rustlang #rust #programming
    #rusttips #rustchallenge

  15. Can you solve this rust code snippet and find output without "running"? - You are the compiler

    (No cheating 😉)
    #rustlang #rust #programming
    #rusttips #rustchallenge #rustlanguage