在这里,我将 vec 拆分为大小相同的 4 子向量,然后返回一个集合,我希望从集合返回的类型为Vec<String>. 我怎么能这样做?
let mut split_operations : Vec<String> = vec[2..].chunks(4).collect::<String>();
这会产生以下错误:
类型的值std::string::String不能从一个迭代过类型的元素来构建&[std::string::String]
类型的值std::string::String不能被构建std::iter::Iterator<Item=&[std::string::String]>
帮助: rustc(E0277)std::iter::FromIterator<&[std::string::String]>没有实现std::string::Stringtrait
我是 Rust 的新手,并且已经了解 Rust 默认为恐慌而不是异常。
我有一个依赖于外部库的 rust 项目。
我已经?使用match语句处理了代码中的所有解包和's ,但我不确定如何panic通过外部库处理 a 。
在其他语言中,我只会捕获库抛出的异常。
由于 Rust 默认为 panics,库不会返回异常,但会panic中止线程的执行。
理想情况下,我希望记录并继续执行,而不是恐慌和中止。
我尝试了以下方法:
catch_unwind,但这看起来像我不能在外部库上使用的东西。log-panicscrate,它使用恐慌钩子记录恐慌。我能够记录恐慌,但不能防止中止。我正在尝试解决一个非常简单的 Rust 练习,涉及PrimitiveDateTime. 我正在尝试在我自己的机器上运行这个基本示例
#![allow(unused)]
extern crate r#time;
fn main() {
use time::{
macros::{date, datetime, time},
PrimitiveDateTime,
};
assert_eq!(
PrimitiveDateTime::new(date!(2019 - 01 - 01), time!(0:00)),
datetime!(2019-01-01 0:00),
);
}
Run Code Online (Sandbox Code Playgroud)
它在 Rust Playground 上编译,但在我的机器上出现以下错误:
$ cargo run
Compiling playground v0.1.0 (/home/sas/exercism/rust/playground)
error[E0432]: unresolved imports `time::macros`, `time::PrimitiveDateTime`, `time::macros::date`, `time::macros::datetime`
--> src/main.rs:4:12
|
4 | use time::{PrimitiveDateTime, macros::{date, datetime, time}};
| ^^^^^^^^^^^^^^^^^ ^^^^^^ ^^^^ ^^^^^^^^
| |
| could not find `macros` in `time`
error: cannot find macro …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行锚构建并收到以下响应:
BPF SDK: /root/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf
Running: rustup toolchain list -v
Running: cargo +bpf build --target bpfel-unknown-unknown --release
error: failed to download `solana-frozen-abi v1.9.4`
Caused by:
unable to get packages from source
Caused by:
failed to parse manifest at `/root/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-frozen-abi-1.9.4/Cargo.toml`
Caused by:
feature `edition2021` is required
consider adding `cargo-features = ["edition2021"]` to the manifest
Run Code Online (Sandbox Code Playgroud)
PS:我已经尝试过建议:Unable to指定`edition2021`以便在Rust中使用不稳定的包
我正在尝试构建一个 Rust 项目,当我尝试cargo build-bpf --manifest-path=contracts/solana/program/Cargo.toml --bpf-out-dir=dist/solana/program在终端中执行时,出现以下错误:
无法获取主目录路径:找不到环境变量
关于如何解决它有什么想法吗?
我正在玩 Rust FFI,我试图将printf具有可变参数的 C 函数与我的 Rust 可执行文件链接起来。运行可执行文件后,我目睹了一些奇怪的行为。
这是我的 Rust 代码:
use cty::{c_char, c_int};
extern "C" {
fn printf(format: *const c_char, ...) -> c_int;
}
fn main() {
unsafe {
printf("One number: %d\n".as_ptr() as *const i8, 69);
printf("Two numbers: %d %d\n".as_ptr() as *const i8, 11, 12);
printf(
"Three numbers: %d %d %d\n".as_ptr() as *const i8,
30,
31,
32,
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是运行后的输出cargo run:
cargo run
Compiling c-bindings v0.1.0 (/home/costin/Rust/c-bindings)
Finished dev [unoptimized + debuginfo] target(s) in 0.20s …Run Code Online (Sandbox Code Playgroud) 这是我的src文件夹结构
src/
|-- main.rs
|-- problems
| |-- mod.rs
| |-- p1.rs
| `-- p2.rs
`-- utilities.rs
Run Code Online (Sandbox Code Playgroud)
我的文件包含我在和utilities.rs中使用的 2 个函数。在 和 中都是行p1.rsp2.rsp1.rsp2.rs
#[path = "../utilities.rs"]
mod utilities;
Run Code Online (Sandbox Code Playgroud)
据我了解,允许我使用中定义的函数 中utilities.rs
定义的两个函数utilities.rs是
pub fn get_lines(num: &str) -> Vec<String> {...}
pub fn split(s: &String, separator: &str) -> Vec<String> {...}
Run Code Online (Sandbox Code Playgroud)
在p2.rs我同时使用它们,但在p1.rs我只使用get_lines. split当我运行或构建时,这会导致货物警告我“从未使用过”的功能,它是。但该函数已被使用,而且不仅在从未调用的函数中,所以我不明白为什么货物会警告我。看来我必须在包含自制模块的每个文件中使用每个模块函数,因为如果我确实调用,split则p1.rs警告就会消失。我不明白什么?
更清楚地说,问题是当我cargo run得到以下内容时
warning: function is never used: `split` …Run Code Online (Sandbox Code Playgroud) 我是区块链新手,我正在使用 solana/anchor/cargo/rust 来构建一个项目。我对这些命令感到困惑。cargo build,cargo build-bpf和有什么区别cargo build-sbf?
在 VSCode 中处理 Tauri 项目时,我从rust-analyzer插件收到以下错误消息。
rust-analyzer failed to discover workspace
Run Code Online (Sandbox Code Playgroud)
我知道问题的原因是我在 VSCode 中加载了 Tauri 项目作为我的工作区,该项目的根目录中没有 a Cargo.toml,而是在 Rust 相关的子目录中src-tauri。有没有办法告诉插件它应该引用这个src-tauri子目录Cargo.toml?src-tauri解决方法是每次接触 Rust 代码时都要切换工作区,这很不方便。
Rust文档提到该#[test]指令用于标记仅在测试模式下编译和执行的函数。#[cfg(test)]那么制定该指令的原因是什么?
rust-cargo ×10
rust ×9
solana ×3
date ×1
datetime ×1
ffi ×1
function ×1
panic ×1
solana-cli ×1
tauri ×1
testing ×1
unit-testing ×1