有没有办法区分Rust中的字符串和标识?

时间:2016-04-23 14:49:08

标签: macros rust

macro_rules! mtc {
    ( $ident:ident ) => ("ident");
    ( $string:expr ) => ("string");
}

fn main() {
    println!("{}", mtc!("hi"));
    println!("{}", mtc!(a));
}

这不起作用。它失败了:

<anon>:7:25: 7:29 error: expected ident, found "hi"
<anon>:7     println!("{}", mtc!("hi"));

1 个答案:

答案 0 :(得分:4)

问题是public class MasjidJamekToBukitBintang extends LetsGoFragment{ private TextView textViewResult; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContectView(R.layout.fragment_result); //route 1 double MJtoPR = 1.20; double PRtoHT = 1.20; double HTtoIM = 1.20; double IMtoBB = 1.20; double route1 = MJtoPR + PRtoHT + HTtoIM + IMtoBB; //route 2 double MJtoDW = 1.00; double DWtoBN = 0.00; double BNtoRC = 1.20; double RCtoBB = 1.20; double route2 = MJtoDW + DWtoBN + BNtoRC + RCtoBB; //route 3 double MJtoPS = 1.00; double PStoKLS = 1.00; double KLStoTS = 1.20; double TStoML = 1.20; double MLtoHT = 1.20; double HTtoIM1 = 1.20; double IMtoBB1 = 1.20; double route3 = MJtoPS + PStoKLS + KLStoTS + TStoML + MLtoHT + HTtoIM1 + IMtoBB1; //shortest path ArrayList al = new ArrayList(); al.add(new Double(route1)); al.add(new Double(route2)); al.add(new Double(route3)); System.out.println("Route choices :"); System.out.println("Route 1 : Masjid Jamek - Plaza Rakyat - Hang Tuah - Imbi - Bukit Bintang"); System.out.println("Fare : RM" + route1); System.out.println("\nRoute 2 : Masjid Jamek - Dang Wangi - Bukit Nanas - Raja Chulan - Bukit Bintang"); System.out.println("Fare : RM" + route2); System.out.println("\nRoute 3 : Masjid Jamek - Pasar Seni - KL Sentral - Tun Sambanthan - Maharaja Lela - Hang Tuah - Imbi - Bukit Bintang"); System.out.println("Fare : RM" + route3); System.out.println("\nSaving cost route : RM" + Collections.min(al)); } private void setContectView(int fragment_result) { } 机器不能退出捕获。一旦启动尝试以匹配捕获,它就会成功或整个宏调用失败

为了做到这一点,你必须提供某种文字匹配之前macro_rules!可以用来区分规则的捕获。例如:

macro_rules!

另外,回答隐含的问题:不,没有办法特定匹配字符串文字,或者实际上任何其他类型的文字。