我有一个 proc-macro 箱,其中有一个宏,当扩展时,需要使用 Rust 内置类型的自定义特征实现。我试图在同一个板条箱中定义该特征,但 Rust 告诉我,一个 proc-macro 板条箱只能有公共宏(用 注释的函数#[proc_macro]),其他任何东西都不能是公共的。因此,我将该特征放入另一个板条箱中,并将其作为依赖项包含在 proc-macro 板条箱中。但这意味着任何想要使用我的 proc-macro 板条箱的人也必须依赖于其他特征板条箱。
所以我想知道是否有一种方法可以向 proc-macro 板条箱添加公共特征,或者以其他方式使 proc-macro 和特征板条箱以某种方式链接起来,以便最终用户无法尝试在没有另一个的情况下使用一个?如果两者都不可行,唯一的解决方案是记录依赖关系,这有点脆弱。
如何在创建的“tests”目录中访问我的库导出函数?
源代码/关系.rs:
#![crate_type = "lib"]
mod relations {
pub fn foo() {
println!("foo");
}
}
Run Code Online (Sandbox Code Playgroud)
测试/test.rs:
use relations::foo;
#[test]
fn first() {
foo();
}
Run Code Online (Sandbox Code Playgroud)
#![crate_type = "lib"]
mod relations {
pub fn foo() {
println!("foo");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我添加了建议的extern crate relations,错误是:
use relations::foo;
#[test]
fn first() {
foo();
}
Run Code Online (Sandbox Code Playgroud)
我想relations在这个单独的tests/test.rs文件中测试我的。我该如何解决这些use问题?
我已经发布了我的箱子,然后意识到我忘了在我的脑袋中加入一些小细节README.md.我已经提供了详细的进 README.md,做git commit和push.如何在不更改版本的情况下更新我的箱子?
cargo yank在您发布实际上因某种原因而最终被破坏的箱子版本(语法错误,忘记包含文件等)时,可能会出现这种情况.对于这种情况,Cargo支持
yank一个版本的板条箱.Run Code Online (Sandbox Code Playgroud)$ cargo yank --vers 1.0.1 $ cargo yank --vers 1.0.1 --undo
我不明白如何使用它.为什么有两个命令:一个没有--undo?我应该同时运行它们吗?还是只有一个?哪一个?
我应该crate package && crate publish追赶吗?还是只cargo yank ...?这会自动更新我的箱子吗?
尝试使用 rust 中的 actix-web 和 mongodb 制作服务器。出现错误
该特征
std::convert::From<mongodb::error::Error>未实现std::io::Error
这是我的代码
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
use mongodb::{options::ClientOptions, Client};
async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
// Parse a connection string into an options struct.
let mut client_options = ClientOptions::parse("mongodb://localhost:27017")?;
// Manually set an option.
client_options.app_name = Some("My App".to_string());
// Get a handle to the deployment.
let client = Client::with_options(client_options)?;
// List …Run Code Online (Sandbox Code Playgroud) 我在从crates.io导入rand crate时遇到问题。添加行 rand="0.8.3" 然后为项目运行命令Cargo build后,它不断显示相同的错误:
error[E0432]: unresolved import `rand`
--> main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing crate `rand`?
error[E0433]: failed to resolve: use of undeclared crate or module `rand`
--> main.rs:4:25
|
4 | let secret_number = rand::thread_rng().gen_range(1..=11);
| ^^^^ use of undeclared crate or module `rand`
error: aborting due to 2 previous errors
Run Code Online (Sandbox Code Playgroud)
Cargo.toml文件
[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"
# See more keys …Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但我似乎无法解决这个问题。
\n我有这样的文件结构:
\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 another.rs\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some_file.rs\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.rs\nRun Code Online (Sandbox Code Playgroud)\n在 中some_file.rs,我想调用 中的函数main.rs。所以,我尝试在some_file.rs:
use crate::main\n\n\nfn some_func() {\n // other code\n \n main::another_func_in_main();\n}\nRun Code Online (Sandbox Code Playgroud)\n但编译器会抛出错误:
\nuse of an undeclared crate or module `main`\nRun Code Online (Sandbox Code Playgroud)\n我该如何解决这个问题?
\n我正在使用 rust 编译和构建一个示例程序。我选择rustc而不是cargo编译,因为它是一个简单的个人测试项目。到目前为止,rustc用于编译和构建可执行文件工作正常,但是当我尝试添加外部rand包时,它给了我这个错误
1 | extern crate rand;
| ^^^^^^^^^^^^^^^^^^ can't find crate
Run Code Online (Sandbox Code Playgroud)
这是完整的代码
extern crate rand;
use rand::Rng;
fn main() {
for x in 1..11 {
let random_number = rand::thread_rng()
.gen_range(1, 101);
println!("{} -> {}", x, random_number)
}
}
Run Code Online (Sandbox Code Playgroud)
如何添加外部包并运行rustc?
我正在尝试导出一个使用包中定义的某些函数的宏。像这样的东西,例如在一个名为的板条箱中a_macro_a_day
// lib.rs
pub fn a() {}
#[macro_export]
macro_rules! impl_helper_funcs {
use crate::a; // error unresolved import
use a_macro_a_day::a; // error unresolved import
fn b() {
...
a() // call imported a here
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用各种组合use来导入a,但错误总是显示宏定义为unresolved import crateor unresolved import a_macro_a_day。
我不想采用过程宏方式,因为这只是为了减少代码重复。有没有办法导出导入本地(但公共)函数的宏?
有没有一种可靠的方法(也许是货物 CLI 中的某些东西)来获取板条箱的版本?
我可以在 Cargo.toml 上进行 grep,但我正在寻找 6 个月内不会损坏的东西。
有没有更好的办法?
所以我对我的代码做了一些更改,并尝试在我的工作计算机上编译它。我收到与 ntapi 包相关的以下错误:
\n error[E0793]: reference to packed field is unaligned\n --> C:\\Users\\u\\.cargo\\registry\\src\\github.com-1eccb9ec823\\ntapi-0.3.6\\src\\ntexapi.rs:2785:52\n |\n2785 | *tick_count.QuadPart_mut() = read_volatile(&(*USER_SHARED_DATA).u.TickCountQuad);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n |\n = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)\n = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw …Run Code Online (Sandbox Code Playgroud) rust ×10
rust-crates ×10
rust-cargo ×5
actix-web ×1
import ×1
macros ×1
mongodb ×1
rust-macros ×1
traits ×1