#rusttips — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #rusttips, aggregated by home.social.
-
🎨 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 settingOr you can add the following to your settings.json:
Link to ref: https://users.rust-lang.org/t/how-to-active-format-on-save-with-rust-analyzer-for-vs-code/41701
-
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.
-
Did you know that a lot of things in #Rust directly implement the `Ord` trait?
For example `Option<T>` where T: Ord
https://doc.rust-lang.org/std/option/enum.Option.html#impl-Ord-for-Option%3CT%3E
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`:
https://doc.rust-lang.org/std/cmp/trait.Ord.html#implementors -
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:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=189efcd9a23ac63b73ccd67bc583c8791/2
-
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:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=129eb419d767b71447b408e456773555This trick is right from the docs on option:
https://doc.rust-lang.org/stable/std/option/#iterating-over-option
What a cunning trick!🤓
Rust, I ❤️ you!
-
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:
Edit: just as I was tooting this, Jon has said it in the stream 😄
-
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
https://github.com/allan2/dotenvy
-
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)
-
✨ Rust Challenge explained
Ans:
2. A gives output and B gives errorIn `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
-
🦀 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
-
✨ 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
-
On the left, the non-idiomatic version ❌
On the right, the more idiomatic version ✔️
2/2
-
Some idiomatic #Rust tips
On the left, the non-idiomatic version ❌
On the right, the more idiomatic version ✔️
1/2
-
13. #RustChallenge
Can you guess the output of this rust code snippet without running it?
-
How to build a Rust API with the builder pattern
https://blog.logrocket.com/build-rust-api-builder-pattern/ -
How to build a Rust API with the builder pattern
https://blog.logrocket.com/build-rust-api-builder-pattern/ -
How to build a Rust API with the builder pattern
https://blog.logrocket.com/build-rust-api-builder-pattern/ -
How to build a Rust API with the builder pattern
https://blog.logrocket.com/build-rust-api-builder-pattern/ -
How to build a Rust API with the builder pattern
https://blog.logrocket.com/build-rust-api-builder-pattern/ -
Can you tell what will be output without running the code?
(Don't forget to mark your answer with spoiler flag)
#rustlang #rustacean #rustaceans #rustlanguage #rust #RustTips #RustTutorial
-
Can you tell what will be output without running the code?
(Don't forget to mark your answer with spoiler flag)
#rustlang #rustacean #rustaceans #rustlanguage #rust #RustTips #RustTutorial
-
Can you tell what will be output without running the code?
(Don't forget to mark your answer with spoiler flag)
#rustlang #rustacean #rustaceans #rustlanguage #rust #RustTips #RustTutorial
-
Can you tell what will be output without running the code?
(Don't forget to mark your answer with spoiler flag)
#rustlang #rustacean #rustaceans #rustlanguage #rust #RustTips #RustTutorial
-
Can you tell what will be output without running the code?
(Don't forget to mark your answer with spoiler flag)
#rustlang #rustacean #rustaceans #rustlanguage #rust #RustTips #RustTutorial
-
More small #RustChallenge will be coming... Stay tuned 🙂
-
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 -
Can you solve this rust code snippet and find output without "running"? - You are the compiler
(No cheating 😉)
What will be the output?
-
Can you solve this rust code snippet and find output without "running"? - You are the compiler
(No cheating 😉)
#rustlang #rust #programming
#rusttips #rustchallenge #rustlanguage -
Resources on reducing the size of your Rust binaries:
https://github.com/johnthagen/min-sized-rust
-
Resources on reducing the size of your Rust binaries:
https://github.com/johnthagen/min-sized-rust
-
Resources on reducing the size of your Rust binaries:
https://github.com/johnthagen/min-sized-rust
-
Resources on reducing the size of your Rust binaries:
https://github.com/johnthagen/min-sized-rust
-
Resources on reducing the size of your Rust binaries:
https://github.com/johnthagen/min-sized-rust