今天我决定学习并开始用 Rust 编码,因为它是一种非常有前途的语言。但是,我尝试在 rust 中编译并运行一个简单的 Hello world 程序,但由于某种原因出现此错误。有人能告诉我出了什么问题吗?
这是我的防锈代码:
fn main(){
println!("Hi");
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
错误:与
cc
失败:退出代码:1 | = 注意:“cc”“-m64”“-L”“/Users/moo7md/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib”“test.test. 7rcbfp3g-cgu.0.rcgu.o" "test.test.7rcbfp3g-cgu.1.rcgu.o" "test.test.7rcbfp3g-cgu.2.rcgu.o" "test.test.7rcbfp3g-cgu.3 .rcgu.o" "test.test.7rcbfp3g-cgu.4.rcgu.o" "test.test.7rcbfp3g-cgu.5.rcgu.o" "-o" "test" "test.5fi6c8ty3hqyycqf.rcgu.o " "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/moo7md/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users /moo7md/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-a5984f6fc2a4870f.rlib"
我正在尝试制作的东西需要插件编译器,因为我需要在编译时报告错误,但是我希望它能在Rust稳定运行而不是每晚都运行.
有没有办法在稳定的Rust上运行编译器插件?
我正在编写一个扩展的Rust编译器插件
choose! {
test_a
test_b
}
Run Code Online (Sandbox Code Playgroud)
至
#[cfg(feature = "a")]
mod test_a;
#[cfg(feature = "b")]
mod test_b;
Run Code Online (Sandbox Code Playgroud)
它现在几乎完成,但模块在最终扩展的代码中不包含任何内容.我猜原因是跨度不包括模块文件.
use syntax::ast;
use syntax::ptr::P;
use syntax::codemap;
use syntax::parse::token;
use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::ext::build::AstBuilder;
use syntax_pos::Span;
use rustc_plugin::Registry;
use syntax::util::small_vector::SmallVector;
// Ideally, it will expand
//
// ```rust
// choose! {
// test_a
// test_b
// }
// ```
// to
// ```rust
// #[cfg(feature = "a")]
// mod test_a;
// #[cfg(feature = "b")]
// mod test_b;
// ``` …
Run Code Online (Sandbox Code Playgroud) 该锈病正则表达式箱提供了regex!
语法扩展,这使得它能够在标准的编译时间编译正则表达式.这有两个好处:
不幸的是,文档说:
警告:该
regex!
编译器插件的幅度比正常慢的顺序Regex::new(...)
使用.您不应该使用编译器插件,除非您有非常特殊的理由这样做.
这听起来像一个完全不同的正则表达式引擎用于regex!
比Regex::new()
.为什么不regex!()
只是Regex::new()
结合两个世界的优势的包装?据我了解,这些语法扩展编译器插件可以执行任意代码; 为什么不Regex::new()
呢?
我想要一个编译器插件来注释带有一些信息的结构.例如,原始结构只有一个字段:
struct X { x: i32 }
Run Code Online (Sandbox Code Playgroud)
我想添加另一个字段:
struct X { x: i32, y: MARKTYPE }
Run Code Online (Sandbox Code Playgroud)
当我查看Rust编译器插件时,我决定使用MultiModifier
(SyntaxExtension
)来完成我的工作.enum ItemKind
定义Struct(VariantData, Generics)
并VariantData
存储数据字段:
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum VariantData {
/// Struct variant.
///
/// E.g. `Bar { .. }` as in `enum Foo { Bar { .. } }`
Struct(Vec<StructField>, NodeId),
/// Tuple variant.
///
/// E.g. `Bar(..)` as in `enum Foo { Bar(..) }`
Tuple(Vec<StructField>, NodeId),
/// Unit …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的 llvm pass 添加到 Rustc 中。Rustc 有一个编译选项-C passes=val
,我们可以在其中添加额外的 LLVM 传递来运行。然而,正如我的尝试,此选项只能在密码放置在 LLVM 代码树内时接受密码,但我想将我的密码从树外添加到 Rustc 中。
当我通过此选项添加我的通行证时:
RUSTFLAGS="-C passes=my-pass" cargo build
编译器报告错误:
error: failed to run LLVM passes: unknown pass name 'my-pass'
然后我尝试通过-C llvm-args=-fpass-plugin=/opt/new-pass/mypass.so -C passes=my-pass
这种clang
方式加载我的通行证。据报道:rustc -Cllvm-args="..." with: Unknown command line argument '-fpass-plugin=/opt/new-pass/mypass.so'
. 也尝试替换-fpass-plugin
为其他选项,如-load
和-load-pass-plugin
,但它们仍然无法被 rustc 识别。
如何将自定义通行证添加到 Rustc 中?
我正在尝试构建一个语法扩展,将属性扩展为调用.之前:
#[flame]
fn flamed() {
..
}
Run Code Online (Sandbox Code Playgroud)
后:
fn flamed() {
flame::start_guard("flamed");
..
}
Run Code Online (Sandbox Code Playgroud)
这已经有效了.但是,如果我#[flame]
在箱子级别(如#![flame]
)具有属性,我也希望它能够工作.这是可能的,如果是的话,怎么样?
使用derive
语法,我可以实现特定字段Hash
或PartialEq
使用特定字段的特征,而不是全部字段吗?
它可能看起来像:
#[derive(Debug, Hash, Eq, PartialEq)]
struct MyStruct {
id: i32,
name: String,
#[derive(hash_skip, eq_skip)]
aux_data1: f64,
#[derive(hash_skip, eq_skip)]
aux_data2: f64,
#[derive(hash_skip, eq_skip)]
aux_data3: String,
}
Run Code Online (Sandbox Code Playgroud)
我希望该hash
方法仅使用id
,name
而不是其他人.
该SERDE库允许这样的事情进行序列化.
我想生成一个HashMap
使用struct字段作为键,并使用usize
整数作为值.
pub struct Article {
title: String,
content: String,
category: String,
comments: Vec<Comment>
}
pub struct Comment {
content: String
}
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:
{
title: 0,
content: 1,
category: 2
comments[].content: 3
}
Run Code Online (Sandbox Code Playgroud)
我的解决办法是impl
我的特质FieldsMapping
两个Article
和Comment
:
pub trait FieldsMapping {
fn get_fields_map(&self) -> HashMap<String, usize>;
}
Run Code Online (Sandbox Code Playgroud)
我想为自定义派生编写一个编译器插件FieldsMapping
.
我的问题是:如何在编译器插件中获取所有字段?我怎么知道字段类型是Vec
什么?