我有一个使用大量堆栈的程序。我使用 Linux,因此已经通过ulimit -s 1048576.
运行cargo test -- --test-threads 1按预期工作,但是当我使用多个线程时,例如cargo test -- --test-threads 2,我得到fatal runtime error: stack overflow. 我相信这是因为运行测试时使用的 Rust 线程默认堆栈大小太小。
运行时如何增加堆栈大小cargo test?
为什么在安装时cargo build --target wasm32-wasi会抛出错误并提示没有安装。\n重现的步骤wasm32-wasi
接下来是控制台的输出
\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) 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句柄。设置hIcon为NULL有效并导致使用标准系统图标。
虽然此方法会生成所需的图标,但图标文件是在执行期间加载的,并且必须与文件一起传递exe。这不是一个可接受的解决方案;图标应链接到exe文件并在其中传递。
如何将图标链接到 Rust Windows 应用程序并在那里使用它?
我知道这个解决方案,但它在编译过程中会生成数千行错误和警告,并且必须被视为过时的。此解决方案有效,但它仅添加Windows 中显示的 …
我收到以下错误:
\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\nRun Code Online (Sandbox Code Playgroud)\n我通过将我的 Rust 版本设置为特定的旧版夜间构建找到了解决方案,但这不适用于另一个项目,我真的想要一个更好的解决方案。我假设我正在使用某些东西的过时版本,或者也许我可以摆脱我的 Cargo.toml 文件中的依赖项,但该错误并没有告诉我有关哪个版本的任何信息。
\n经过一番搜索后,我尝试运行cargo tree -e features -i proc-macro2,但是虽然输出了很多涉及多个依赖项的内容,但没有任何具体涉及proc_macro_span_shrink.
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) 我得到了这个货物 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 文件,这是不允许/不可能的?
包含所有内容的目录称为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 { …
为什么这段代码会给我带来奇怪的结果?
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, …
我创建了一个简单的hello world程序:
fn main() {
println!("Hello, world");
}
Run Code Online (Sandbox Code Playgroud)
使用rustcvs 编译代码时cargo build,cargo命令显示较慢.它需要1.6秒为cargo buildVS 1S的rustc.请参阅屏幕截图右侧的时间戳.
为什么是这样?我为什么还要用货?
我有一个前端项目,在src文件夹中有很多东西,我有机会在服务器端使用Rust.我的所有Rust服务器文件都在server文件夹中; 我怎么能告诉Cargo运行./server/app.rs?
在Linux上编译生锈rustc或cargo 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`
rust ×10
rust-cargo ×10
linux ×1
rust-crates ×1
rust-wasm ×1
rustup ×1
testing ×1
winapi ×1
windows ×1