我可以针对实现Pattern的通用类型创建匹配吗? [英] Can I create a match against a generic type that implements Pattern?

查看:45
本文介绍了我可以针对实现Pattern的通用类型创建匹配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为,如果我能算出生命周期表示法,这可能会奏效,我错了吗?

I think this might work if I can work out the lifetime notation, am I wrong?

pub fn from<T:Pattern>(from: T) -> Result<Tag, &'static str> {
    match from {
        'A'|"A" => Ok(Tag::ChA),
        'B'|"B" => Ok(Tag::ChB),
        'C'|"C" => Ok(Tag::ChC),
        'D'|"D" => Ok(Tag::ChD),
        'T'|"Tmpr" => Ok(Tag::Tmpr),
        'Y'|"Batt" => Ok(Tag::Batt),
        'L'|"Acc" => Ok(Tag::Acc),
        _ => Err("Error: unknown channel"),
    }
}

推荐答案

匹配仅适用于具体类型,因此答案是否定的.除了我们可以调用 Pattern 中定义的方法(用于搜索字符串)之外,这里对 T 的类型一无所知.

Matching only works with concrete types, so the answer is no. Nothing is known about the type T here except that we can call the methods defined in Pattern on it (which are for searching in a string).

这篇关于我可以针对实现Pattern的通用类型创建匹配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆