mirror of
https://github.com/Noratrieb/blog.git
synced 2026-01-14 20:35:02 +01:00
Update item-patterns-and-struct-else.md
This commit is contained in:
parent
6bda6f7246
commit
00a8e092ec
1 changed files with 30 additions and 28 deletions
|
|
@ -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?
|
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!
|
||||||
|
|
||||||
```
|
```rust
|
||||||
const WITH_LOGS: bool = cfg!(with_logs);
|
struct fn input() -> u8 {
|
||||||
|
const INPUT: &str = prompt!();
|
||||||
|
const Ok(INPUT): Result<u8, ParseIntErr> = INPUT.parse() else {
|
||||||
|
compile_error!("Invalid input");
|
||||||
|
};
|
||||||
|
INPUT
|
||||||
|
}
|
||||||
|
|
||||||
struct A = if const WITH_LOGS {
|
const RANDOM: u8 = env!("RANDOM");
|
||||||
{ name: String, logs: Logs }
|
|
||||||
} else {
|
|
||||||
{ name: String }
|
|
||||||
};
|
|
||||||
|
|
||||||
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 {
|
fn main() {
|
||||||
i32 => 8,
|
// Empty. I am useless.
|
||||||
i64 => 4,
|
}
|
||||||
_ => panic!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
type OptionU8 = Option<u8>;
|
|
||||||
|
|
||||||
struct Some(Person) = (if enum Option<u8> = OptionU8 {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
{ name: String }
|
|
||||||
}) else {
|
|
||||||
compile_error!("NO!");
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
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`
|
||||||
|
|
||||||
<sub>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.</sub>
|
```rust
|
||||||
<br/>
|
struct Some(Test) = None else {
|
||||||
<sub>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</sub>
|
compile_error!("didn't match pattern");
|
||||||
|
};
|
||||||
|
|
||||||
|
<sub>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</sub>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue