标签: rust-cargo

如何增加“cargo test”使用的线程的堆栈大小?

我有一个使用大量堆栈的程序。我使用 Linux,因此已经通过ulimit -s 1048576.

运行cargo test -- --test-threads 1按预期工作,但是当我使用多个线程时,例如cargo test -- --test-threads 2,我得到fatal runtime error: stack overflow. 我相信这是因为运行测试时使用的 Rust 线​​程默认堆栈大小太小。

运行时如何增加堆栈大小cargo test

testing rust rust-cargo

1
推荐指数
1
解决办法
1578
查看次数

安装时可能无法安装“wasm32-wasi”目标

为什么在安装时cargo build --target wasm32-wasi会抛出错误并提示没有安装。\n重现的步骤wasm32-wasi

\n
    \n
  1. 货运新列车
  2. \n
  3. rustup 目标添加 wasm32-wasi
  4. \n
  5. 货物构建--目标 wasm32-wasi
  6. \n
\n

接下来是控制台的输出

\n
\xe2\x9e\x9c  train git:(master) \xe2\x9c\x97 cargo build --target wasm32-wasi\n   Compiling train v0.1.0 (/user/playground/rustLang/train)\nerror[E0463]: can't find crate for `std`\n  |\n  = note: the `wasm32-wasi` target may not be installed\n  = help: consider downloading the target with `rustup target add wasm32-wasi`\n\nerror: cannot find macro `println` in this scope\n --> src/main.rs:2:5\n  |\n2 |     println!("hello");\n  |     ^^^^^^^\n\nerror: requires `sized` lang_item\n\nFor more information about this …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo rustup rust-wasm

1
推荐指数
1
解决办法
2079
查看次数

如何将图标链接到 Rust Windows 应用程序?

struct在 Rust 桌面应用程序中,总是使用某些版本的窗口,例如WNDCLASSW. WNDCLASSW定义后,可以通过成员添加类图标。下面的代码摘录演示了如何包含存储在文件中的图标。structhIconIcon.ico

...
        let hicon: HICON = LoadImageW(
            0 as HINSTANCE, 
            wide_null("Icon.ico").as_ptr(), 
            IMAGE_ICON, 0, 0, LR_LOADFROMFILE
        ) as HICON;

        let hinstance = GetModuleHandleW(null_mut());

        let mut wc: WNDCLASSW = std::mem::zeroed();
        wc.lpfnWndProc = Some(window_proc);
        wc. hInstance = hinstance;
        wc.hIcon = hicon;
        wc.lpszClassName = name.as_ptr();
...
Run Code Online (Sandbox Code Playgroud)

图标文件在程序执行期间加载,并且必须与该exe文件保存在同一文件夹中。如果图标文件丢失,LoadImageW()则返回NULL句柄。设置hIconNULL有效并导致使用标准系统图标。

虽然此方法会生成所需的图标,但图标文件是在执行期间加载的,并且必须与文件一起传递exe。这不是一个可接受的解决方案;图标应链接到exe文件并在其中传递。

如何将图标链接到 Rust Windows 应用程序并在那里使用它?

我知道这个解决方案,但它在编译过程中会生成数千行错误和警告,并且必须被视为过时的。此解决方案有效,但它仅添加Windows 中显示的 …

windows winapi rust rust-cargo

1
推荐指数
1
解决办法
872
查看次数

如何判断为什么需要“未知功能”功能?

我收到以下错误:

\n
    error[E0635]: unknown feature `proc_macro_span_shrink`\n  --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.50/src/lib.rs:92:30\n   |\n92 |     feature(proc_macro_span, proc_macro_span_shrink)\n   |                              ^^^^^^^^^^^^^^^^^^^^^^\n\nFor more information about this error, try `rustc --explain E0635`.\nerror: could not compile `proc-macro2` (lib) due to previous error\n
Run Code Online (Sandbox Code Playgroud)\n

我通过将我的 Rust 版本设置为特定的旧版夜间构建​​找到了解决方案,但这不适用于另一个项目,我真的想要一个更好的解决方案。我假设我正在使用某些东西的过时版本,或者也许我可以摆脱我的 Cargo.toml 文件中的依赖项,但该错误并没有告诉我有关哪个版本的任何信息。

\n

经过一番搜索后,我尝试运行cargo tree -e features -i proc-macro2,但是虽然输出了很多涉及多个依赖项的内容,但没有任何具体涉及proc_macro_span_shrink.

\n
proc-macro2 v1.0.50\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 quote v1.0.17\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 syn v1.0.90\n\xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 proc-macro-error v1.0.4\n\xe2\x94\x82           \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 proc-macro-error feature "default"\n\xe2\x94\x82           \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 clap_derive v4.1.8 (proc-macro)\n\xe2\x94\x82           \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 clap_derive feature "default"\n\xe2\x94\x82           \xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 clap v4.1.8\n\xe2\x94\x82 …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo rust-proc-macros

1
推荐指数
1
解决办法
393
查看次数

如果 main.rs 位于工作区中的同一个 crate 中且包名称等于 bin 名称,则无法添加对 lib.rs 的依赖项

我得到了这个货物 toml 文件:

[package]
edition.workspace = true
name = "batch_jobs"
version.workspace = true

[[bin]]
name = "batch_jobs"
path = "src/main.rs"

[lib]
name = "batch_jobs_lib"
path = "src/lib.rs"
Run Code Online (Sandbox Code Playgroud)

在另一个板条箱中,我想添加对batch_jobs_lib. 我无法这样做。货物抱怨找不到板条箱。当我更改该部分的命名时[[bin]],它可以正常工作,但我只是想知道这段代码出了什么问题。

在同一工作区的另一个板条箱中,我尝试像这样声明依赖项:

batch_jobs_lib = { path = "../batch_jobs" }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:error: no matching package named batch_jobs_lib found

使用此代码时,我无法访问该库的代码:

batch_jobs = { path = "../batch_jobs" }
Run Code Online (Sandbox Code Playgroud)

更糟糕的是,Rust 甚至找不到任何包,但很乐意添加依赖项。

我是否遗漏了某些内容,或者我是否错误配置了我的 toml 文件,这是不允许/不可能的?

rust rust-cargo

1
推荐指数
1
解决办法
56
查看次数

我有一个“use”不起作用,即使它在我的 src 中

包含所有内容的目录称为p,位于p

.git
.gitignore
Cargo.lock
Cargo.toml
todo.md
src
target
Run Code Online (Sandbox Code Playgroud)

src

action_functions.rs
execute_action_functions.rs
lib.rs
main.rs
network.r
Run Code Online (Sandbox Code Playgroud)

network.rs

pub mod network{
        use rand::Rng    //to generate random numbers
    //use crate::action_functions::{/*insert all the action functions */};
    use rand_distr::{Normal, Distribution}; //to generate different dist. of random numbers
    use serde::{Serialize, Deserialize};    //to save/load my neural network
    use std::fs::File;
    use std::io::{BufReader, BufWriter};
    use chrono::Utc;            //for timestamp
    use std::path::Path;        //for help in representing file paths

Run Code Online (Sandbox Code Playgroud)

然后我有一堆 pub 结构,然后我有impl NeuralNetwork { …

rust rust-crates rust-cargo rust-analyzer

1
推荐指数
1
解决办法
124
查看次数

Rust匹配意外的结果

为什么这段代码会给我带来奇怪的结果?

const VAR1: u16 = 1;
const VAR2: u16 = 2;
const VAR3: u16 = 3;
const VAR4: u16 = 4;

#[cfg(test)]
mod tests {
    // use {VAR1, VAR2, VAR3, VAR4};
    #[test]
    fn test_match() {
        let a = 4;
        match a {
            VAR1 => {
                println!("matched: 1");
            }
            VAR2 => {
                println!("matched: 2");
            }
            VAR3 => {
                println!("matched: 3");
            }
            VAR4 => {
                println!("matched: 4");
            }
            _ => {
                println!("not matched");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

// use {VAR1, VAR2, …

rust rust-cargo

0
推荐指数
1
解决办法
111
查看次数

如果"货物构建"比直接运行rustc慢,我为什么要使用Cargo?

我创建了一个简单的hello world程序:

fn main() {
    println!("Hello, world");
}
Run Code Online (Sandbox Code Playgroud)

使用rustcvs 编译代码时cargo build,cargo命令显示较慢.它需要1.6秒cargo buildVS 1Srustc.请参阅屏幕截图右侧的时间戳.

为什么是这样?我为什么还要用货?

rust rust-cargo

0
推荐指数
1
解决办法
1107
查看次数

如何告诉Cargo从"src"以外的目录运行文件?

我有一个前端项目,在src文件夹中有很多东西,我有机会在服务器端使用Rust.我的所有Rust服务器文件都在server文件夹中; 我怎么能告诉Cargo运行./server/app.rs

rust rust-cargo

0
推荐指数
1
解决办法
1246
查看次数

Rust没有编译成Linux中的可执行文件

在Linux上编译生锈rustccargo build生成共享库而不是可执行文件.
我的文件管理器(thunar)和file命令显示该文件类型为共享库.

编译后的二进制文件只能通过$ /path/to/file或通过终端执行$ cargo run.
该文件无法通过双击执行,因为其他可执行文件可以.命令
输出file:

$ file rust_bin

rust_bin:ELF 64位LSB共享对象,x86_64,版本1(SYSV),动态链接,解释器/lib64/ld-linux-x86-64.so.2,适用于GNU/Linux 3.2.0,BuildID [sha1] = cb8cd ...,with debug_info,not stripped`

linux rust rust-cargo

0
推荐指数
1
解决办法
575
查看次数