Jan :rust: :ferris: · @janriemer
517 followers · 2992 posts · Server floss.social

When reading arguments of when to use Associated Types (AT) vs. Generics (G) in , 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

1/2

#Rust #rustlang #rusttips

Last updated 2 years ago

Jan :rust: :ferris: · @janriemer
513 followers · 2898 posts · Server floss.social

Oh wow, did you know that in 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!

#Rust #RustTip #rusttips #rustlang

Last updated 2 years ago

Jan :rust: :ferris: · @janriemer
508 followers · 2720 posts · Server floss.social

BinaryHeap in 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

#Rust #rusttips #rustlang

Last updated 2 years ago

Astra Kernel :verified: · @AstraKernel
1052 followers · 1348 posts · Server infosec.exchange

🦀

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

#rusttips #rustlang #rust #infosec #appsec

Last updated 2 years ago

Astra Kernel :verified: · @AstraKernel
1044 followers · 1327 posts · Server infosec.exchange
Astra Kernel :verified: · @AstraKernel
1020 followers · 1274 posts · Server infosec.exchange

🦀

What is the output of this?

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

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

#rustquiz #RustChallenge #rustlang #rust #rusttips #learnrust

Last updated 2 years ago

Astra Kernel :verified: · @AstraKernel
999 followers · 1226 posts · Server infosec.exchange

✨ 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

Last updated 3 years ago

Astra Kernel :verified: · @AstraKernel
997 followers · 1213 posts · Server infosec.exchange

✨ 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

Last updated 3 years ago

Astra Kernel :verified: · @AstraKernel
995 followers · 1210 posts · Server infosec.exchange

🦀 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

Last updated 3 years ago

Astra Kernel :verified: · @AstraKernel
991 followers · 1206 posts · Server infosec.exchange

✨ 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

Last updated 3 years ago

Jan :rust: :ferris: · @janriemer
435 followers · 1508 posts · Server floss.social
Jan :rust: :ferris: · @janriemer
435 followers · 1507 posts · Server floss.social
Astra Kernel :verified: · @AstraKernel
630 followers · 671 posts · Server infosec.exchange

13.

Can you guess the output of this rust code snippet without running it?

#RustChallenge #rustlang #rustprogramming #rustaceans #rusttips #rustacean

Last updated 3 years ago

Astra Kernel :verified: · @AstraKernel
462 followers · 505 posts · Server infosec.exchange
Astra Kernel :verified: · @AstraKernel
435 followers · 483 posts · Server infosec.exchange
Astra Kernel :verified: · @AstraKernel
276 followers · 381 posts · Server infosec.exchange
Astra Kernel :verified: · @AstraKernel
255 followers · 351 posts · Server infosec.exchange

More small will be coming... Stay tuned 🙂

#RustChallenge #rustlang #rustlanguage #rusttips

Last updated 3 years ago

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

Last updated 3 years ago

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

Last updated 3 years ago

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

(No cheating 😉)

#rustlang #rust #programming #rusttips #RustChallenge #rustlanguage

Last updated 3 years ago