Crates.io | rollz |
lib.rs | rollz |
version | 0.1.1 |
source | src |
created_at | 2021-04-10 19:21:53.288107 |
updated_at | 2021-04-11 14:49:48.869094 |
description | A super-simple Rust crate that allows you to... roll dice! What else would you need from a crate? |
homepage | |
repository | https://github.com/lmammino/rollz |
max_upload_size | |
id | 381764 |
size | 14,636 |
rollz
is a super-simple Rust crate that allows you to... roll dice! What else would you need from a crate?!
In reality, this crate was born as an experiment for me to understand Rust return type polymorphism and while I was at it, why wouldn't I publish it as a crate for everyone else enjoyment!?
I don't expect this to be particularly useful for real life projects™️, except maybe if you are implementing something that has to do with D&D or similar types of board games.
Yes, this crate implements standard D&D dice set: D4
, D6
, D8
, D10
, D12
and D20
.
To install the library add the following lines to your Cargo.toml
:
[dependencies]
rollz = "0"
Or, if you have cargo add
, you can run the following command:
cargo add rollz@0
This is how you would roll a D10
with rollz
:
use rollz::prelude::*;
use rollz::D10;
fn main() {
let d: D10 = roll();
println!("You got a {}", d.val()); // You got a 2
}
Or you can also roll multiple dices together, because why not?
use rollz::prelude::*;
use rollz::D6;
fn main() {
let d: (D6, D6) = (roll(), roll());
println!("You got {:?}", d); // You got (D6(4), D6(6))
}
More usage examples are available in the examples
folder.
Here's an example for you, if you want to build a custom die:
use rollz::prelude::*;
/// A roll of this guy will always give you 100!
/// Shush ... Don't tell anyone! 🤫
#[derive(Debug)]
struct Fake100(u8);
impl Rollable for Fake100 {
fn roll() -> Fake100 {
Fake100 { 0: 100 }
}
fn val(&self) -> u8 {
self.0
}
}
fn main() {
println!("I bet I'll get a 100 this time!");
let d: Fake100 = roll();
println!("Look what I got: {}!", d.val())
}
Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.
Licensed under MIT License. © Luciano Mammino.