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:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a2c5a0e080a4fc56bb9ea8accac22a8b

#RustLang #RustTips #Strings

Rust Playground

A browser interface to the Rust compiler to experiment with the language

🎨 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: https://users.rust-lang.org/t/how-to-active-format-on-save-with-rust-analyzer-for-vs-code/41701

#RustTips #CodeStyle #Rust30by30 #Day12

How to active format on save with rust-analyzer for VS Code?

I have used rust-analyzer on VS Code for a while now but I cannot find any settings key related to format on save.

The Rust Programming Language Forum

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.  

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

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

#RustLang #RustTips #RustTip

Option in std::option - Rust

The `Option` type. See the module level documentation for more.

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. ✨  

See this playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=189efcd9a23ac63b73ccd67bc583c879

#RustLang #RustTips

1/2

Rust Playground

A browser interface to the Rust compiler to experiment with the language

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!

#RustTip #RustTips #RustLang

Rust Playground

A browser interface to the Rust compiler to experiment with the language

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:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=98725f05ebcaf2c7fa373b2d7bf93b12

#RustTips #RustLang

Edit: just as I was tooting this, Jon has said it in the stream πŸ˜„

Rust Playground

A browser interface to the Rust compiler to experiment with the language

πŸ¦€ #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

https://github.com/allan2/dotenvy

https://rustsec.org/advisories/RUSTSEC-2021-0141.html

#rustlang #rust #infosec #appsec

GitHub - allan2/dotenvy: A well-maintained fork of the Rust dotenv crate

A well-maintained fork of the Rust dotenv crate. Contribute to allan2/dotenvy development by creating an account on GitHub.

GitHub

πŸ¦€ #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