Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/augustt198/macros
https://github.com/augustt198/macros
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/augustt198/macros
- Owner: augustt198
- Created: 2014-10-19T19:11:24.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-29T22:43:09.000Z (about 10 years ago)
- Last Synced: 2023-08-02T15:35:23.972Z (over 1 year ago)
- Language: Rust
- Size: 160 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# macros
A collection of rust macros.
### Conditionals
The `maybe!` macro returns a random boolean when called with empty parameters:
```rust
let rand: bool = maybe!();
```When called with a block/expression, the block/expression is called if a random boolean is true:
```rust
maybe! {
println!("50%-50% chance")
}
```### Repetition
The `repeat!` macro is called with the format: `repeat! { times }`.
Multiple ` times` groupings can be placed together in a `repeat!` macro, when seperated by a comma.```rust
repeat! {
println!("Double, double, toil and trouble") 3 times
}
```The `twice!` macro calls the supplied block/expression twice:
```rust
twice! {
println!("Happy Birthday to You")
}
```### Hash literal
The `hash!` macro creates an `std::collections::HashMap` from `key => value` pairs:
```rust
let hash = hash! {"January" => 0u, "February" => 1, "December" => 11};
```### Ternary operator
Ternary operators have been removed from rust, but the `t!` macro can be used as a substitute using this syntax:
```rust
t! { conditional ? true_branch : false_branch }
```For example:
```rust
let val = t! { maybe!() ? "yay" : "nay" }
```