愚人节恶作剧:Rust的“goto”实现


厌倦了使用“loop”、“while”和“for”等新奇的控制流机制?
好了不用担心了!
终于,Rust 的“goto”和“label”宏已经到来!他们是#![no_std]!

use goto_label::{goto, label};

#[no_mangle] // Needed to prevent foo() from being optimized away
unsafe fn foo() {
    println!(
"This text will never be printed!");

    label!(
"label1");
    print!(
"Hello");
    goto!(
"label2");

    println!(
"Neither will this be printed!");
}

unsafe fn hello_world() {
    goto!(
"label1");
    println!(
"This won't be printed either!");

    label!(
"label2");
    println!(
" World!");
}

unsafe {
    hello_world();
}

警告:
不要实际使用,它肯定会导致未定义的行为,很可能表现为segfaults。