如何在一个可执行文件中组合不同的算法



为了学习Rust,我已经开始实现Project Euler中的一些问题。现在我想进行下一步,创建一个基于控制台的用户界面,它能够运行所有或仅运行特定问题。另一个要求是,用户应该能够只将可选参数传递给特定的问题。

我目前的解决方案是有一个Trait ProjectEulerProblem声明例如run()。我可以这样做:

fn main() {
    let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());
    let problems: Vec<Box<problems::ProjectEulerProblem>> = vec![
        box problems::Problem1,
        box problems::Problem2
    ];
    match args.flag_problem {
        Some(x) => println!("Result of problem: {} is {}", x, problems[x-1].run()),
        None    => println!("No problem number given.")
    }
}

我的问题是,有没有办法摆脱明确的problems矢量初始化,也许通过使用宏?也欢迎其他实现我的应用程序的想法,如上面所描述的。

您可以使用带有重复的宏来生成列表,而不必每次都输入完整的路径和名称。

macro_rules! problem_vec(
    ($( $prob:tt ),*) => ({
        &[
            $(
                &concat_idents!(Proble, $prob),
            )*
        ]
    });
);
const PROBLEMS: &'static[&'static ProjectEulerProblem] = problem_vec!(m1, m2);

注意,不能简单地使用索引,因为concat_idents宏需要一个标识符,而数字不是标识符。concat_idents也只在夜间可用。在stable中,您需要给出整个结构的名称:

macro_rules! problem_vec(
    ($( $prob:ident ),*) => ({
        &[
            $(
                &problems::$prob,
            )*
        ]
    });
);
const PROBLEMS: &'static [&'static problems::ProjectEulerProblem] = problem_vec!(
    Problem1, Problem2
);

游戏围栏

我的 mashup crate允许您定义一种简洁的方法来构建问题数组problems![1, 2]。此方法适用于任何Rust版本>= 1.15.0。


#[macro_use]
extern crate mashup;
mod problems {
    pub trait ProjectEulerProblem {
        fn run(&self);
    }
    pub struct Problem1;
    impl ProjectEulerProblem for Problem1 {
        fn run(&self) {
            println!("running Project Euler problem 1");
        }
    }
    pub struct Problem2;
    impl ProjectEulerProblem for Problem2 {
        fn run(&self) {
            println!("running Project Euler problem 2");
        }
    }
}
macro_rules! problems {
    ($($number:tt),*) => {{
        // Use mashup to define a substitution macro `m!` that replaces every
        // occurrence of the tokens `"Problem" $number` in its input with the
        // concatenated identifier `Problem $number`.
        mashup! {
            $(
                m["Problem" $number] = Problem $number;
            )*
        }
        // Invoke the substitution macro to build a slice of problems. This
        // expands to:
        //
        //     &[
        //         &problems::Problem1 as &problems::ProjectEulerProblem,
        //         &problems::Problem2 as &problems::ProjectEulerProblem,
        //     ]
        m! {
            &[
                $(
                    &problems::"Problem" $number as &problems::ProjectEulerProblem,
                )*
            ]
        }
    }}
}
fn main() {
    for p in problems![1, 2] {
        p.run();
    }
}

最新更新