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!
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
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 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
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/
Can you tell what will be output without running the code?
(Don't forget to mark your answer with spoiler flag)
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