diff --git a/content/posts/item-patterns-and-struct-else.md b/content/posts/item-patterns-and-struct-else.md index 7f3c963..3535691 100644 --- a/content/posts/item-patterns-and-struct-else.md +++ b/content/posts/item-patterns-and-struct-else.md @@ -218,38 +218,40 @@ We can go further. Today, items are just there with no ordering. What if we imposed an ordering? What if "rust items" was a meta scripting language? -Imagine this program: +We can write a simple guessing game! -``` -const WITH_LOGS: bool = cfg!(with_logs); +```rust +struct fn input() -> u8 { + const INPUT: &str = prompt!(); + const Ok(INPUT): Result = INPUT.parse() else { + compile_error!("Invalid input"); + }; + INPUT +} -struct A = if const WITH_LOGS { - { name: String, logs: Logs } -} else { - { name: String } -}; +const RANDOM: u8 = env!("RANDOM"); -compile_println!("Hello, world!"); +loop { + const INPUT = input(); + if INPUT == RANDOM { + break; // continue compilation + } else if INPUT < RANDOM { + compile_warn!("input is smaller"); + } else { + compile_warn!("input is bigger"); + } +} -const INT_WIDTH: u8 = match struct libc::c_int { - i32 => 8, - i64 => 4, - _ => panic!(), -}; - -type OptionU8 = Option; - -struct Some(Person) = (if enum Option = OptionU8 { - None -} else { - { name: String } -}) else { - compile_error!("NO!"); -}; +fn main() { + // Empty. I am useless. +} ``` -And then we end up at the logical conclusion: `struct else`. +And then, last but not least I want to highlight one of my favourite consequences of this: `struct else` -maybe this post was meant as a joke. maybe it wasn't. it is up to you to bring your own judgement to the idea and write an RFC. -
-this post was not meant to make fun of anyone's ideas. it was just a good bad idea i had once and then friends made me write this +```rust +struct Some(Test) = None else { + compile_error!("didn't match pattern"); +}; + +this post was not meant to make fun of anyone's ideas. it was just a good idea i had once and then friends made me write this