Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Rust Utility Macros
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Libraries
Rust Utility Macros
Commits
e3c7c819
Commit
e3c7c819
authored
1 month ago
by
Louis
Browse files
Options
Downloads
Patches
Plain Diff
Add README.md
parent
ec4705ad
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
README.md
+54
-0
54 additions, 0 deletions
README.md
with
54 additions
and
0 deletions
README.md
0 → 100644
+
54
−
0
View file @
e3c7c819
# Utility Macros
## Usage
### Add to your project
```
toml
[dependencies]
# There is not presently a crates.io release
weirdboi_utils
=
{
git
=
"https://weirdboi.dev/libraries/weirdboi-utils.git"
,
rev
=
"put-a-revision-here"
}
```
### Build Collections
Build hashmaps in-place with the
`hashmap`
macro
```
rust
let
some_output
=
function_call
(
"string"
,
23
,
hashmap!
{
"key"
=>
123
,
"another_key"
=>
456
});
```
### Safe Unwrapping
When you want to simply do nothing and cease further execution without a panic, the convenience
`some!`
and
`ok!`
macros are handy:
```
rust
fn
do_something_without_fail
(
value
:
Result
<
A
,
B
>
,
maybe
:
Option
<
F
>
)
{
let
value
=
ok!
(
value
);
let
maybe
=
some!
(
maybe
);
println!
(
"Value was OK and maybe was SOME"
);
}
```
Alternatively, in a loop you can skip the rest of the current iteration instead of ending executino entirely:
```
rust
fn
do_many_things
(
list
:
impl
Iter
<
Item
=
Option
<
F
>>
)
{
for
item
in
list
{
let
current
=
some!
(
item
;
continue
);
println!
(
"Found SOME: {}"
,
current
);
}
}
```
Finally, if the function has a return type then you should specify a default value to return:
```
rust
fn
get_widget
(
maybe
:
Result
<
A
,
B
>
)
->
i32
{
let
maybe
=
ok!
(
maybe
;
-
1
);
// Returns "-1" from the function if maybe is Err(_)
}
```
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment