Skip to content

Commit 88a38ec

Browse files
committed
feat: rpt 33 - struct initializers are functions
1 parent f44e901 commit 88a38ec

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

content/blog/rust-pro-tips-collection.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ license:
1212

1313
This is a collection of Rust "pro tips" that I've collected, most of which have been [posted on Twitter](https://twitter.com/search?q=%23RustProTip%20%40sudo_build&src=typed_query&f=top). I'll keep updating this post as I write more. Tips are ordered in reverse chronological order, with the most recent ones at the top.
1414

15+
## 33. Use tuple struct initializers as function pointers
16+
17+
<!-- [Tweet]() [Toot]() -->
18+
19+
Tuple struct initializers can be cast to function pointers. This can help to avoid creating unnecessary lambda functions, e.g. when calling [`Iterator::map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map).
20+
21+
```rust
22+
#[derive(Debug, PartialEq, Eq)]
23+
struct Point(i32, i32);
24+
25+
fn zeroes<T>(f: fn(i32, i32) -> T) -> T {
26+
f(0, 0)
27+
}
28+
29+
assert_eq!(zeroes(Point), Point(0, 0));
30+
```
31+
32+
[Docs](https://doc.rust-lang.org/reference/expressions/struct-expr.html#tuple-struct-expression) \
33+
[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7cb12bc6930a9ae03204a33eb50529ce)
34+
1535
## 32. Absolute import paths
1636

1737
[Tweet](https://twitter.com/sudo_build/status/1733063054840262842) [Toot](https://infosec.exchange/@hatchet/111544198990772125)
@@ -25,7 +45,7 @@ pub mod std {
2545
pub enum Ordering {
2646
Equal,
2747
}
28-
48+
2949
impl Ordering {
3050
pub fn is_eq(self) -> bool {
3151
panic!("Bamboozled!");

0 commit comments

Comments
 (0)