I don’t think do yeet is useful

I don’t think do yeet is useful because all it does is return an error early

fn do_yeeting() -> Result<(), &str> {
    do yeet "error";
    Ok(())
}

is the same as:

fn not_yeeting() -> Result<(), &str> {
    return Err("error");
    Ok(())
}

It makes code shorter but an even shorter way to write it is:

fn also_not_yeeting() -> Result<(), &str> {
    Err("error")?;
    Ok(())
}

it also makes more sense and is easier to read

Yeah, while it's not exactly the same as return Err(…) (because in a try block it exits the block, not the enclosing function), I have to generally agree that giving the same concept an (N+1)-th different syntax is unnecessary feature bloat.

(But most probably, this sort of discussion belongs on IRLO, not here.)

4 Likes

I also feel like it would be better if it was just yeet instead of do yeet

do is temporary. It's because yeet is a placeholder, not a real keyword. It can get a dedicated keyword in the next edition.

5 Likes

Will it though? I don't remember seeing anything about that for the 2024 edition...

Can you elaborate on what yeet exactly does? The docs aren’t really explaining it in detail. And why try-catch you mean match?

Some next-next edition after it's selected for stabilization.

There's an experimental feature called try blocks, intended to make it easier to work with Result. The idea is that the last expression in the block gets wrapped in Ok and is the value of the block (so try { 6 } is the same as Ok(6)), return inside the block still leaves the function, ? causes the block's value to be the Err case if there is one, and yeet is bikeshed syntax for ending the block immediately and making the block's value an Err case.

The intended use is to write something like:

let file_sum = try {
    if filename == "/etc/passwd" {
        do yeet Errors::NotThatDumb;
    }
    let file = std::io::BufReader::new(std::fs::File::open(filename)?);
    let mut sum = 0u64;
    for line in file.lines() {
        let line = line?;
        sum += line.parse::<u64>()?;
    }
    sum
};

Where you still have access to "good" error handling, via ?, you can use the yeet syntax to throw a special error case, but you don't need to Ok-wrap your return value, and you don't have to deal with either Result combinators or with trying to not return early from the function as a whole.

9 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.