Skip to content

Instantly share code, notes, and snippets.

@YoshiTheChinchilla
Last active September 8, 2020 05:18
Show Gist options
  • Save YoshiTheChinchilla/33c119006ba621efca3a56833b68ac20 to your computer and use it in GitHub Desktop.
Save YoshiTheChinchilla/33c119006ba621efca3a56833b68ac20 to your computer and use it in GitHub Desktop.
Making a roasted sweet potato in Rust https://qiita.com/mattn/items/d447a43ad12fada71663
[package]
name = "bonfire"
version = "0.1.1"
authors = ["Takashi Yoshimura"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "bonfire"
path = "lib.rs"
[dependencies]
rand = "0.7.3"
// cargo add bonfire --git https://gist.github.com/YoshiTheChinchilla/33c119006ba621efca3a56833b68ac20
// Or use this text of dependency: `bonfire = { git = "https://gist.github.com/YoshiTheChinchilla/33c119006ba621efca3a56833b68ac20" }`
// Rust Playground URL: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=08f68ee479d41442dc4cc1a3645a6837
use std::fmt::{self, Display, Formatter};
pub const HOKUIMO: &[char] = &['ホ', 'ク', 'イ', 'モ'];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SweetPotato {
pub state: [[char; 2]; 2],
}
impl SweetPotato {
/// Makes a roasted sweet potato
#[inline]
pub fn new() -> Self {
Self {
state: [Self::random(), Self::random()]
}
}
/// Makes a roasted sweet potato with chars
#[inline]
pub fn new_with(x: [char; 2], y: [char; 2]) -> Self {
Self {
state: [x, y]
}
}
/// Makes a tasty roasted sweet potato
#[inline]
pub fn new_completed() -> Self {
Self {
state: [['ホ', 'ク'], ['イ', 'モ']],
}
}
/// Makes roasted sweet potatoes until a tasty roasted sweet potato is made
#[inline]
pub fn new_until_completed() -> Vec<Self> {
let mut v = Vec::with_capacity((3 * 4usize).pow(2));
loop {
let sweet_potato = Self::new();
let is_tasty = sweet_potato.is_tasty();
v.push(sweet_potato);
if is_tasty { break }
}
v.shrink_to_fit();
v
}
/// Generates two random HOKUIMO chars
#[inline]
pub fn random() -> [char; 2] {
use rand::seq::IteratorRandom;
let mut rng = rand::thread_rng();
let mut buf = [' '; 2];
HOKUIMO.iter().copied().choose_multiple_fill(&mut rng, &mut buf);
buf
}
/// Returns true if the sweet potate is tasty
#[inline]
pub fn is_tasty(&self) -> bool {
self.state[0] == ['ホ', 'ク'] && self.state[1] == ['イ', 'モ']
}
}
impl Display for SweetPotato {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let [h, t] = self.state;
write!(f, "{0}{1}{0}{1}の{2}{3}", h[0], h[1], t[0], t[1])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sweet_potato() {
// new, Display
let sweet_potato = SweetPotato::new();
let chars = sweet_potato.to_string().chars().collect::<Vec<_>>();
assert_eq!(chars.len(), 7);
assert_eq!(chars[0..2], chars[2..4]);
assert_eq!(chars[4], 'の');
// random
let random = SweetPotato::random();
assert!(random.len() == 2);
assert!(HOKUIMO.contains(&random[0]) && HOKUIMO.contains(&random[1]));
// new_with, new_completed, is_tasty
assert!(!SweetPotato::new_with(['ク', 'ホ'], ['モ', 'イ']).is_tasty());
assert!(SweetPotato::new_completed().is_tasty());
// new_until_completed
let mut sweet_potatoes = SweetPotato::new_until_completed();
let tasty_sweet_potato = sweet_potatoes.pop().unwrap();
assert!(tasty_sweet_potato.is_tasty());
assert_eq!(tasty_sweet_potato.to_string(), "ホクホクのイモ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment