[][src]Crate candy

Ever wanted to suck? Here, have some sweet sugared candy.

This crate provides some syntaxic sugar for Rust, in the shape of macros.

Examples

//! Run with `cargo run --example do_loop -- <number>`

#[macro_use] extern crate candy;

use ::std::*;

type ErrorMsg = borrow::Cow<'static, str>;

fallible! {
fn main ()
    ->  ()
    =>! ErrorMsg
:
    let input_number: u64 = {
        let (mb_argv_0, mb_argv_1) = {
            let mut args = env::args();
            (args.next(), args.next())
        };
        let prog_name = mb_argv_0.unwrap();
        match mb_argv_1
                .and_then(|argv_1| argv_1.parse().ok())
        {
            Some(number) => number,
            _ => throw!(format!("Usage: {} <number>", prog_name)),
        }
    };
    collatz_conjecture(input_number);
}

fn collatz_conjecture (mut n: u64)
{
    do_loop!({
        println!("n = {}", n);
        if n % 2 == 0 {
            n /= 2;
        } else {
            n = 3 * n + 1;
        };
    } while n != 1);
    println!("Did reach 1.");
}
//! Run with `cargo run --example catch`

#[macro_use] extern crate candy;

use ::std::{
    *,
    io::Write,
};

fn main ()
{
    debug_print_all([0b101010, 0x45].iter())
}

fn debug_print_all (
    iterable: impl IntoIterator<Item = impl fmt::Debug>,
)
{
    let to_stdout = &mut io::stdout();

    // `catch!` allows using the `?` operator. Isn't that nice?
    match catch!({
        write!(to_stdout, "[")?;
        let mut iterator = iterable.into_iter();
        let mut count = 0;
        if let Some(first) = iterator.next() {
            count += 1;
            write!(to_stdout, "{:?}", first)?;
            while let Some(next) = iterator.next() {
                count += 1;
                write!(to_stdout, ", {:?}", next)?;
            };
        };
        write!(to_stdout, "]\n")?;
        count
    } -> usize =>! io::Error)
    {
        Err(io_err) => {
            eprintln!(
                "{} : could not write to stdout!? Oh well, who cares?",
                io_err,
            );
        },
        Ok(n) => {
            eprintln!("Successfully wrote {} elements to stdout", n);
        },
    }
}

Macros

catch

Sugar for an expression block catching early-returns.

debug_assert

See this thread or this issue.

debug_assert_eq

See this thread or this issue.

debug_assert_ne

See this thread or this issue.

default

Sugar for Default::default().

do_loop

Sugar for the do { ... } while (...); C construct in Rust.

fallible

Sugar for the pervasive -> Result<T, E> fallible return type pattern.

not

Enhances the readability of the negation unary operator (!).

ret

Sugar for early-returning a success: return Ok($ok_value.into())

throw

Sugar for early-returning an error: return Err($err_value.into())