标签: rust-cargo

测试时出现“调用上下文未设置错误”是怎么回事?

当我在此代码上运行 Cargo test-bpf --manifest-path=./Cargo.toml 时

#[tokio::test]
async fn test_init_mapping() {
    let program_id = Pubkey::new_unique();
    let mut init_map_test = ProgramTest::new(
        "simple", // Run the BPF version with `cargo test-bpf`
        program_id,
        processor!(process_instruction), 
    );
    let main_pubkey = Pubkey::new_unique();
    let main_account = Account {
        lamports: 100,
        owner: program_id,
        ..Account::default()
    };
    init_map_test.add_account(main_pubkey, main_account);
    let (mut banks_client, payer, recent_blockhash) = init_map_test.start().await;
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误。

线程“test_init_mapping”因“调用上下文未设置!”而惊慌失措。**

我不知道如何调试这个,所以任何帮助都很棒。我已将问题范围缩小到最后一行。我的项目中一定有其他地方设置错误吗?

我不认为这段代码有问题,因为它是从 helloworld 示例中复制粘贴的。

编辑:我留下了原始问题的重要细节。在最后一行之后,我收到了 msg!("started"); ,我认为无关紧要,所以将其排除在外

rust rust-cargo solana

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

错误:无法为 `syn` 选择版本

error: failed to select a version for `syn`. \
    ... required by package `serde_derive v1.0.125`\
    ... which satisfies dependency `serde_derive = "=1.0.125"` of package `serde v1.0.125`    
    ... which satisfies dependency `serde = "^1.0.125"` of package `mongodb v2.1.0`\
    ... which satisfies dependency `mongodb = "^2.1"` of package `wagmeet v0.1.0 
 \(/mnt/e/College/Eighth Semester/Crypto_Capable/wagmeet_app)`\
versions that meet the requirements `^1.0.60` are: 1.0.86, 1.0.85, 1.0.84, 1.0.83, 1.0.82, 1.0.81, 1.0.80, 1.0.79, 1.0.78, 1.0.77, 1.0.76, 1.0.75, 1.0.74, 1.0.73, 1.0.72, 1.0.71, 1.0.70, 1.0.69, 1.0.68, 1.0.67, 1.0.66, 1.0.65, 1.0.64, …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo rust-diesel rust-tokio nearprotocol

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

RUST:有没有更优雅的方式来导入 mod?

这是我的项目结构:

\n
.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sub_folder\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 mod.rs\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sub_mod.rs\n
Run Code Online (Sandbox Code Playgroud)\n

在 中sub_mod.rs,如果我像这样导入,则货物不会警告我sub_folder/

\n
.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sub_folder\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 mod.rs\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sub_mod.rs\n
Run Code Online (Sandbox Code Playgroud)\n

但我做不到

\n
#[path = "./sub_folder/mod.rs"]\nmod sub_folder;\n
Run Code Online (Sandbox Code Playgroud)\n

但它main.rs有效!

\n

有没有更温和的方式sub_mod.rs导入sub_folder/

\n

rust rust-cargo rust-analyzer

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

Cargo:如何将整个目录或文件包含在功能标志中?

我正在开发一个 Rust 项目。我正在使用 Cargo 功能标志来对某些代码进行条件编译。在某些情况下,我必须将整个文件包含在功能标志中,因此这样做添加#[cfg(feature="my-flag")]每个函数和use语句没有多大意义。

因此,为了将整个文件包含在功能标志中,我想将文件中的所有内容包围在一个块中并为该块添加功能标志。

#[cfg(feature="my-flag")]
{
  use crate::access_control::{func1, func2};
  use crate::models;
  ...

  #[derive(Debug)]
  pub enum MyEnum{..}
  
  #[derive(Clone)]
  pub Struct MyStruct{..}

  pub fn my_func() {...}
  fn my_func_internal() {...}
  ...
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误Syntax Error: expected an item after attributes

另外,在某些情况下,我希望将整个目录包含在功能标志中。我该怎么办?为每个文件添加功能标志是一种方法。是否存在更好的方法?

conditional-compilation rust rust-cargo

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

打印!导致OpenGL程序无法运行?

我目前正在用 Rust 开发一个简单的程序,在屏幕上绘制一个彩色三角形。我按照OpenGL 的说明进行操作。我的程序运行良好,三角形按预期呈现:

在此输入图像描述

之后,我尝试抽象代码: 和VAO成为VBO处理ShaderProgram绑定、附加着色器等的结构。那行得通。现在解决问题:

当我尝试抽象属性时遇到了问题。我将这两个方法添加到结构中ShaderProgram,它们应该处理属性的启用,而不必对诸如步幅或属性在向量中的位置开始的指针之类的内容进行硬编码。

pub fn add_attribute(&mut self, name: &'a str, length: i32) {
    let mut props = VertexAttributeProps::new(name, length);
    self.attributes.push(props);
}
pub fn apply_attributes(&self) {
    const GL_FLOAT_SIZE: i32 = mem::size_of::<gl::types::GLfloat>() as i32;
    let stride = self
        .attributes
        .iter()
        .fold(0, |prev, attr| prev + attr.length)
        * GL_FLOAT_SIZE;

    let mut current_pos: i32 = 0;
    for attr_options in self.attributes.iter() {
        // println!("{:?}", current_pos);
        unsafe {
            let attribute = VertexAttribute::new( …
Run Code Online (Sandbox Code Playgroud)

opengl glfw rust rust-cargo

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

如何避免 Rust 项目中的依赖地狱?

我正在开发一个依赖于许多第 3 方板条箱的项目。\n正如经常发生的那样,第 3 方板条箱依赖于其他板条箱。

\n

我经常发现自己处于这样的情况:两个或多个板条箱需要同一个板条箱的不同版本。因此,cargo 无法选择版本。\n通常,有问题的 crate 是我没有明确添加的 crate。

\n

同时,唯一的解决方案是检查第三方并更新那里的版本,但这通常需要更新整个第三方链。这需要时间而且不好玩。当我尝试将更改发布到上游时,需要很长时间才能合并它们。

\n

Rust 有没有更好的方法来处理这种情况?

\n

PS 我有 C++ 背景,其中依赖管理更糟糕。所以,我对现代生态系统没有太多经验,可能会错过一些东西。

\n

为了提供更多上下文,我添加了部分(完整版本很长)cargo tree输出。

\n

这是我添加新依赖项之前的树:

\n
    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 near-chain-configs v0.0.0 (https://github.com/f-squirrel/nearcore.git?rev=76424c004de84f6b5823751f132f62a0da9aa656#0a8e7ac7)\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 anyhow v1.0.75 (*)\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 chrono v0.4.26 (*)\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 derive_more v0.99.17 (proc-macro) (*)\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 near-config-utils v0.0.0 (https://github.com/f-squirrel/nearcore.git?rev=76424c004de84f6b5823751f132f62a0da9aa656#0a8e7ac7)\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 anyhow v1.0.75 (*)\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 json_comments v0.2.1\n    \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 thiserror v1.0.47 (*)\n    \xe2\x94\x82 …
Run Code Online (Sandbox Code Playgroud)

dependencies rust rust-crates rust-cargo

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

禁用“注意:使用 `RUST_BACKTRACE=1` 环境变量运行以显示回溯”消息

当 Rust 发生恐慌时,如何禁用“注意:使用RUST_BACKTRACE=1环境变量运行以显示回溯”?我的代码:

use std::{env, fs, path::Path};
fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 3{
        panic!("Incorrect args!");
    }
    let query: &String = &args[1];
    let path: &Path = Path::new(&args[2]);
    println!("Search: \"{query}\"\nPath: \"{}\"", path.to_str().unwrap());
    match path.exists(){
        true => (),
        false => {panic!("Invalid file path!");}
    }
    println!("Success");
}

Run Code Online (Sandbox Code Playgroud)

panic rust rust-cargo

0
推荐指数
2
解决办法
297
查看次数

在Cargo项目中使用板条箱时出现错误,提示“也许缺少外部板条箱”

我今天开始学习Rust,但是在这一步上我陷于困境。我想在项目中使用兰特板条箱,因此我Cargo.toml按照教程中的建议进行了更新:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]

[dependencies]
rand = "0.3.14"
Run Code Online (Sandbox Code Playgroud)

在我的代码中将其导入为:

use rand::Rng;
Run Code Online (Sandbox Code Playgroud)

它给出了这个错误:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]

[dependencies]
rand = "0.3.14"
Run Code Online (Sandbox Code Playgroud)

我想念什么吗?


edition = "2018"按照建议添加:

Cargo.toml:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]
edition = "2018"

[dependencies]
rand = "0.3.14"
Run Code Online (Sandbox Code Playgroud)

货运量现在可以提供:

use rand::Rng;
Run Code Online (Sandbox Code Playgroud)

我使用更新了rust rustup update,然后将其添加extern crate rand;到main.rs中。现在它正在按预期工作。

该程序运行,但是在我的vscode问题选项卡中,它仍然显示错误-

error[E0432]: unresolved …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo

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

为什么会出现这个错误?我们不能在函数括号内使用格式宏吗?

代码:

fltk::frame::Frame::new(0,0, 300, 100, format!("side item {}", i));
Run Code Online (Sandbox Code Playgroud)

输出错误:

the trait `std::convert::From<std::string::String>` is not implemented for `std::option::Option<&'static str>`
Run Code Online (Sandbox Code Playgroud)

fltk rust rust-cargo

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

Traitdiesel::Expression 未针对 NaiveDate 实现,但适用于 NaiveDateTime

我正在尝试用作chrono::NaiveDate数据库模型字段。这是模型:

\n
use chrono::{NaiveDate, NaiveDateTime};\nuse diesel::{Insertable, Queryable};\nuse serde::{Deserialize, Serialize};\n\nuse crate::schema::users;\n\n#[derive(Debug, Serialize, Deserialize, Associations, Identifiable, Queryable)]\n#[table_name = "users"]\npub struct User {\n    pub id: uuid::Uuid,\n    pub password_hash: String,\n    pub is_active: bool,\n\n    pub is_premium: bool,\n    pub premium_expiration: Option<NaiveDate>,\n\n    pub email: String,\n    pub first_name: String,\n    pub last_name: String,\n    pub date_of_birth: NaiveDate,\n    pub currency: String,\n\n    pub modified_timestamp: NaiveDateTime,\n    pub created_timestamp: NaiveDateTime,\n}\n\n#[derive(Debug, Insertable)]\n#[table_name = "users"]\npub struct NewUser<'a> {\n    pub id: uuid::Uuid,\n    pub password_hash: &'a str,\n    pub is_active: bool,\n\n    pub is_premium: bool,\n    pub premium_expiration: Option<NaiveDate>,\n\n …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo rust-chrono rust-diesel

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

为什么“cargo test”在运行我的测试之前说“运行 0 个测试”?

我是 Rust 新手,目前正在尝试为现有脚本的重构版本创建测试用例。我注意到,出于某种原因,每当我运行时cargo test,我首先收到消息“运行 0 个测试”,然后“运行 x 个测试”(其中 x 是我编写的测试数)。

如何找到“运行 0 次测试”的来源?

testing unit-testing rust rust-cargo

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

-2
推荐指数
2
解决办法
480
查看次数