Top Tweets for #RustProTip
#RustProTip №38: The debug_assert! macro
The debug_assert! macro creates an assertion only in debug builds. It is useful for confirming assumptions during development without risking performance degradation in the release build.

#RustProTip №37: Block labels
Implicit returns from blocks aren't always flexible enough to describe the desired logic. Separate functions aside, the problem can be resolved inline by introducing a block label.
Playground: https://t.co/F8IRrfrGME
RFC 2046: https://t.co/0w15Ta5Vqe
All Rust Pro Tips: https://t.co/SjUDaL4X0T
#rust #rustlang #programming

#RustProTip №36: Specify the type of self in method signatures
While the 3 most common types of the self parameter have shorthands (self, &self, &mut self), the explicit syntax allows stdlib types that deref to Self, including Box, Rc, Arc.
#rust #rustlang #programming

#RustProTip №35: Force compilation failure
The compile_error!(...) macro forces compilation failure. This can be useful to indicate unsupported feature flag combinations or invalid macro arguments.
(see blog post for 2 bonus tips!)
#rust #rustlang #programming

#RustProTip №33: Use tuple struct initializers as function pointers
Tuple struct initializers can be cast to function pointers. This can help to avoid creating unnecessary lambda functions, e.g. when calling Iterator::map.
#rust #rustlang #programming

#RustProTip №32: Absolute import paths
Use a leading double colon (::path) to indicate a path relative to the root of the compilation unit. This can help to avoid name collisions when writing macros.
#rust #rustlang #programming

#RustProTip №31: Use indirection in enums to save memory
All variants of an enum are the same size. This can become a problem when variants have drastically different memory requirements. Use indirection (&-reference, Box, etc.) to resolve.
#rust #rustlang #programming

#RustProTip №30: Create a slice from a reference without copying
If you have a reference to some data and you want to pass it to a function that takes a slice, you can use `std::array::from_ref` to cheaply create a slice without copying.
#rust #rustlang #programming

#RustProTip №28: Workspace dependencies
Easily specify a uniform dependency version for all crates in a workspace with the [workspace.dependencies] table in Cargo.toml.
#rust #rustlang #programming
![sudo_build's tweet photo. #RustProTip №28: Workspace dependencies
Easily specify a uniform dependency version for all crates in a workspace with the [workspace.dependencies] table in Cargo.toml.
#rust #rustlang #programming https://t.co/jY9F4gGx4q](https://pbs.twimg.com/media/F3UaG5SbsAABuAK.png)
#RustProTip №27: Test for compilation failure
Create tests intended to fail compilation with the `compile_fail` attribute on documentation tests.
#rust #rustlang #programming

#RustProTip №26: Sealed traits
If a public trait requires the implementation of a private trait, the public trait is “sealed” and can only be implemented within the crate that defines it.
#rust #rustlang #programming

#RustProTip
Use std::mem::transmute to statically assert that two types have the same size at compile time.
#rust #rustlang #programming #developers

#RustProTip
Use the `cfg` and `cfg_attr` attributes to compile different code based on the build environment. This is useful for feature-gating, platform-specific code, etc.
#rust #rustlang #programming #developers

#RustProTip
Using rust-analyzer on VSCode, place your cursor over an `fn` keyword to highlight all exit points of that function.
#rust #rustlang #programming

#RustProTip
Declaratively construct HashMaps from iterators of key-value tuples.
#rust #rustlang #programming #developers

#RustProTip
Use impl for generic monomorphization, dyn for dynamic dispatch.
In the generated assembly, with_impl is generated once for each parameterization. with_dyn is generated once, but a vtable is required at runtime.
#rust #rustlang #programming

#RustProTip
Rule of thumb: the more a closure does with captured variables, the fewer traits it implements.
Only reads? Fn + FnMut + FnOnce.
Mutates? FnMut + FnOnce.
Moves? FnOnce.
Quiz: Which traits does `f` implement in each sample / Which lines cause compilation errors?

#RustProTip
Require tests to panic with `#[should_panic]`. This is useful for testing unhappy paths. Optionally include a substring to match against the panic message.
![sudo_build's tweet photo. #RustProTip
Require tests to panic with `#[should_panic]`. This is useful for testing unhappy paths. Optionally include a substring to match against the panic message. https://t.co/jaxeE6tDGV](https://pbs.twimg.com/media/FuLP6PCakAAW95K.jpg)
#RustProTip
Nobody asked for it, and you're getting it!
https://t.co/td4aVxBGNI
#RustProTip
One of Rust's lesser-known composite types is the union. While they might look like structs at first, each field is actually the same piece of memory, allowing you to reinterpret bytes as a different type. Of course, this requires `unsafe` code.

Most Popular Users

Elon Musk 
@elonmusk
240.2M followers

Barack Obama 
@barackobama
119.3M followers

Donald J. Trump 
@realdonaldtrump
111.6M followers

Cristiano Ronaldo 
@cristiano
109M followers

Narendra Modi 
@narendramodi
107M followers

Rihanna 
@rihanna
97.3M followers

NASA 
@nasa
92.1M followers

Justin Bieber 
@justinbieber
90.6M followers

KATY PERRY 
@katyperry
86.9M followers

Taylor Swift 
@taylorswift13
80.7M followers

Lady Gaga 
@ladygaga
72.2M followers

Kim Kardashian 
@kimkardashian
69.4M followers

YouTube 
@youtube
68.6M followers

Virat Kohli 
@imvkohli
68.6M followers

Bill Gates 
@billgates
63.4M followers

The Ellen Show
@theellenshow
62.5M followers

CNN 
@cnn
61.9M followers

Neymar Jr 
@neymarjr
61.2M followers

X 
@x
60.9M followers

Selena Gomez 
@selenagomez
60M followers

![sudo_build's tweet photo. #RustProTip №28: Workspace dependencies
Easily specify a uniform dependency version for all crates in a workspace with the [workspace.dependencies] table in Cargo.toml.
#rust #rustlang #programming https://t.co/jY9F4gGx4q](https://pbs.twimg.com/media/F3UaFZEaMAAOZHs.png)





