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=189efcd9a23ac63b73ccd67bc583c879
1/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=129eb419d767b71447b408e456773555
This 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!
#Rust #RustTip #rusttips #rustlang
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:
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
#rusttips #rustlang #rust #infosec #appsec
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)
#rustquiz #RustChallenge #rustlang #rust #rusttips #learnrust
✨ 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
✨ Yes, the answer is C
It prints:
-10
+100
🎯 What is the use of `+` in format string?
👉 By default, number sign is not printed for positive numbers
👉 We use `+` to print sign also
print!("{:+} {:+}", 1, -2);
It will print
+1 -2
#rustlang #rusttips #rust #learnrust
🦀 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
#rustlang #rusttips #rust #learnrust
✨ 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
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?
#RustChallenge #rustlang #rustprogramming #rustaceans #rusttips #rustacean
Creating a Command-Line Application in Rust
https://www.joshfinnie.com/blog/a-command-line-application-in-rust/
Credit: @joshfinnie
#rust #rustlang #rustaceans #rusttutorial #rusttips
How to build a Rust API with the builder pattern
https://blog.logrocket.com/build-rust-api-builder-pattern/
#rust #rusttutorial #rusttips #rustaceans #rustlang
Can you tell what will be output without running the code?
#rustlang #rustacean #rustaceans #rustlanguage #rust #RustTips #RustTutorial
#RustChallenge #rustlang #rustacean #rustaceans #rustlanguage #rust #rusttips #rusttutorial
More small #RustChallenge will be coming... Stay tuned 🙂
#RustChallenge #rustlang #rustlanguage #rusttips
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 #rustchalleng #rustlanguage
#rustlang #rust #programming #rusttips #rustchalleng #rustlanguage
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
Can you solve this rust code snippet and find output without "running"? - You are the compiler
(No cheating 😉)
#rustlang #rust #programming
#rusttips #rustchallenge #rustlanguage
#rustlang #rust #programming #rusttips #RustChallenge #rustlanguage