我有从protobuf定义生成的模块文件。有很多文件,它们会随着时间的推移而增长。结构是这样的:
proto_rust/src/lib.rs
proto_rust/src/protos/{lots of auto generated .rs files}
proto_rust/src/protos/mod.rs
由于proto_rust/src/protos/中有很多文件,因此我没有必要手动将它们放入mod.rs中。有没有办法把它们都暴露给lib.rs?就像pub mod *一样。
在Rust中,声明宏和过程宏(macro_rules!)都可以接受不一定有效的任意语法,只要它解析。例如,,它用于创建一个宏,该宏从类似SQL的语法中解析SQL查询。
然而,在使用属性宏时,情况似乎并非如此。例如:
#[my_attribute_macro]
fn example() {
impl A {
impl B {
// This is invalid Rust code (nested `impl`s) which is parsable
}
}
}
生成编译器错误,因为impls不能嵌套在有效的Rust中。是否
一个非常简单的例子是在Rust宏中实现基本的加法和乘法。
compute!(1 + 2 * 3) // should evaluate to 7
我不完全确定这是可能的,因为Rust宏的语法有限。
这里的重点不是在编译时计算某些内容,而是能够以某种方式解析标记(具有优先级):
(term, terms*) => { parse_mul!(term) + (parse_mul!(terms))* } // this is not actual Rust!
我正在尝试编写一个宏,以确定各种函数执行的时间。
macro_rules! timer {
($( $x: expr ),+ ) => {
let now = SystemTime::now();
let val = $x;
match now.elapsed() {
Ok(elapsed) => {
// This should include a message at some point alongside the timing
p
从Rust 1.0开始,无法将多个模式分组为一个绑定:
// It does not compile
match x as char {
b @ ('A' | 'Z') => println!("A or Z: {}", b),
_ => println!("Try again")
}
// Correct version
match x as char {
b @ 'A' | b @ 'Z' => println!("A or Z: {}"
我有一个模块csv,正在尝试使用外部csv库(use csv::ReaderBuilder;)。我有点矛盾。
显示错误的最小代码
mod csv {
use ::csv::ReaderBuilder;
}
错误消息
--> src/csv.rs:4:5
|
4 | use csv::ReaderBuilder;
| ^^^ help: a similar path exists: `crate::csv`
|
= note: `use` statements changed in Rust 2018; read more at <https://d
我想创建默认类型的泛型结构。但是,Rust编译器仍然要求我在创建结构时指定显式类型。
struct A {}
struct C<T = A> {
t: Option<T>
}
fn main() {
let c = C { t: None };
}
锈蚀编译器显示此错误:
error[E0282]: type annotations needed for `C<T>`
--> src/main.rs:8:9
|
8 | let c = C { t: None };
| - ^ cannot infer type
使用Rust 2018,此代码工作():
use std::panic;
use std::format;
use std::assert_eq;
但这一点:
use std::assert;
导致此错误:
error[E0432]: unresolved import `std::assert`
--> src/lib.rs:4:5
|
4 | use std::assert;
| ^^^^^^^^^^^ no `assert` in the root
我读过,它说use应该与macro_rules!宏和过程宏一起工作。因此,我很困惑。
在L系统表示法中,一个模式看起来像这样: A(a)<A(x)>B(b, c) if a+b+c < 10 => B(a+b, a+c)A(x+a+b+c) 我正在尝试编写rust宏来扩展这些。所以我有这样的东西: macro_rules! test {
($lc:pat < $a:pat > $rc:pat) => { ... };
} 但它不让我这么做。上面写着: error: `$a:pat` is followed by `>`, which is not allowed for `pat` fragments
--> s
我正在尝试弄清楚如何从Ruby解析器(https://github.com/kenaniah/ruby-parser/blob/master/ruby-parser/src/parsers/expression/logical.rs)的Rust端口中的逻辑关键字表达式中删除间接的左递归。语法看起来像这样: E --> N | A | O | t
N --> n E
A --> E a E
O --> E o E E = expression
A = keyword_and_expression
O = keyword_or_expression
N = keyword
我正在创建一个命令行解析器,并希望支持选项捆绑。但是,我不确定如何处理可能出现的歧义和冲突。考虑以下三种情况:
1.
-I accepts a string
"-Iinclude" -> Would be parsed as "-I include"
2.
-I accepts a string
-n accepts an integer
"-Iincluden10" -> Would be parsed as "-I include -n 10" because the 'cluden10' af