Vector 可以创建一个引用惰性静态值的向量吗?
编译以下代码:Vector 可以创建一个引用惰性静态值的向量吗?,vector,static,rust,Vector,Static,Rust,编译以下代码: let x = Regex::new(r"\d+").unwrap(); let y = Regex::new(r"asdf\d+").unwrap(); let regexes = vec![x, y]; 但该代码不: lazy_static! { static ref X_PRIME: Regex = Regex::new(r"\d+").unwrap(); static ref Y_PRIME: Regex = Regex::new(r"asdf\d+")
let x = Regex::new(r"\d+").unwrap();
let y = Regex::new(r"asdf\d+").unwrap();
let regexes = vec![x, y];
但该代码不:
lazy_static! {
static ref X_PRIME: Regex = Regex::new(r"\d+").unwrap();
static ref Y_PRIME: Regex = Regex::new(r"asdf\d+").unwrap();
}
let regexes = vec![X_PRIME, Y_PRIME];
错误是:
错误[E0308]:类型不匹配
-->src\syntax\lex.rs:19:33
|
19 |让正则表达式=vec![X_素数,Y_素数];
|^^^^^^应为结构'syntax::lex::lex::X_PRIME',找到结构'syntax::lex::lex::Y_PRIME'`
|
=注意:应为'syntax::lex::lex::X_PRIME'类型`
=注意:找到类型`syntax::lex::lex::Y_PRIME`
是。lazy_static提供了X_PRIME
和Y_PRIME
不同的类型,但它们都是,因此您可以编写:
let regexes = vec![&*X_PRIME, &*Y_PRIME];
// The * dereferences the values to a `Regex` type
// The & turn them back into references `&Regex`.
您还可以定义另一个静态:
lazy_static! {
static ref X_PRIME: Regex = Regex::new(r"\d+").unwrap();
static ref Y_PRIME: Regex = Regex::new(r"asdf\d+").unwrap();
static ref REGEXES: Vec<&'static Regex> = vec![&X_PRIME, &Y_PRIME];
}
lazy\u static!{
静态引用X_PRIME:Regex=Regex::new(r“\d+”).unwrap();
静态引用Y_PRIME:Regex=Regex::new(r“asdf\d+”).unwrap();
静态引用正则表达式:Vec