我有一个HashMap
端点:
#[derive(Debug, Clone, Copy, Ord, Eq, PartialOrd, PartialEq, Hash)]
enum Endpoint {
OauthToken,
Disciplines,
PublicTournaments,
MyTournaments,
Matches,
}
lazy_static! {
static ref API_EP: HashMap<Endpoint, &'static str> = {
let mut m: HashMap<Endpoint, &'static str> = HashMap::new();
m.insert(Endpoint::OauthToken, "/oauth/v2/token");
m.insert(Endpoint::Disciplines, "/v1/disciplines");
m.insert(Endpoint::PublicTournaments, "/v1/tournaments");
m.insert(Endpoint::MyTournaments, "/v1/me/tournaments");
m.insert(Endpoint::Matches, "/v1/tournaments/{}/matches");
m
};
}
正如您所看到的,可以通过Endpoint::Matches
键访问的地图项返回一个需要格式化的字符串,但是,我没有看到这样做的方法。
我尝试使用format!
宏,但它需要字符串文字而不是对象,所以它不起作用。我试图通过互联网搜索解决方案并生锈std库但我无法找到它。有没有办法做到这一点?
为了澄清为什么format!
不起作用:
error: format argument must be a string literal.
--> src/lib.rs:249:31
|
249 | let address = format!(get_ep_address(Endpoint::Matches)?, id.0);
get_ep_address(Endpoint::Matches)?
不是字符串文字,因此无法正常工作。
我还想知道如何格式化&str
和String
类型。
答案 0 :(得分:2)
builder.Entity<Exemption>()
.HasIndex(c => new { c.IdNumber, c.Period.Id }).IsUnique();
宏需要一个文字因为它调用编译器魔术(format_args!
)来检查编译时的格式并确保其参数实现必要的特征。例如,The property 'Period' cannot be added to the entity type 'Exemption' because a navigation property with the same name already exists on entity type 'Exemption'.
要求参数实现format!
,如Formatting traits中所述。
你可能能够通过直接联系Formatter
来拼凑一些东西,但这意味着你自己解析{}
。
注意:Rust不具有反射功能,因此检查类型是否在运行时实现给定的特征并不容易......