小编pho*_*ina的帖子

如何使用Serde在反序列化期间转换字段?

我使用SERDE反序列化具有十六进制值的XML文件0x400作为一个字符串,我需要将其转换为数值1024u32.

我是否需要实现Visitor特性,以便将0x分离,然后将400从基数16解码到基数10?如果是这样,我该怎么做才能使基数10整数的反序列化保持不变?

hex rust serde

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

如何在 Rust 的内联汇编宏中使用常量?

我正在将一些遗留汇编代码移植到 Rust,我需要通过asm!宏调用它。然而,汇编代码依赖于 C 头文件中存储的一些常量。我想保持类似,在 Rust 中定义常量,并在宏中包含常量的名称asm

旧版 C 标头:

#define HCR_VALUE 0xffff0000
Run Code Online (Sandbox Code Playgroud)

旧版 ASM 文件:

.func
...
ldr    x0, =HCR_VALUE
...
Run Code Online (Sandbox Code Playgroud)

铁锈代码:

pub const HCR_VALUE: u32 = 0xffff0000;
Run Code Online (Sandbox Code Playgroud)
unsafe { asm!("ldr x0, HCR_VALUE":::"x0"); }
Run Code Online (Sandbox Code Playgroud)

构建应用程序最终会出现链接器错误:

lld-link: error: undefined symbol: HCR_VALUE
Run Code Online (Sandbox Code Playgroud)

inline-assembly rust arm64

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

如何将LLVM后端参数传递给Xargo?

我正在为AArch64架构编译Rust应用程序,我需要传递LLVM后端参数,-mgeneral-regs-only以便代码只使用通用寄存器.

当我需要交叉编译应用程序时,如何将参数传递给Xargo?

正如所建议的那样,我尝试运行该命令,RUSTFLAGS但收到有关未知命令行参数的错误:

RUSTFLAGS='-C llvm-args=-mgeneral-regs-only' xargo build --target aarch64-unknown-none
error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `rustc - --crate-name ___ --print=file-names -C llvm-args=-mgeneral-regs-only --sysroot /home/.xargo -Z force-unstable-if-unmarked --target aarch64-unknown-none --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro` (exit code: 1)
--- stderr
rustc: Unknown command line argument '-mgeneral-regs-only'.  Try: 'rustc -help'
rustc: Did you mean '-mark-data-regions'?
Run Code Online (Sandbox Code Playgroud)

llvm rust

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

如何在 Cargo 配置中为构建和测试指定不同的默认目标?

我正在为 AArch64 目标交叉编译 Rust 裸机应用程序,我需要在 x86_64 目标(我的 PC)上运行单元测试。

我创建了文件.cargo/config

[build]
target = "aarch64-unknown-none"
Run Code Online (Sandbox Code Playgroud)

我想为 AArch64 构建,但要为 x86_64 运行测试。如果我将构建更改为x86_64-unknown-linux-gnu然后测试编译和执行。有没有我可以指定的部分?我现在必须手动交换这些。

我检查了货物指南,但没有找到有关测试配置的参考。

rust rust-cargo

5
推荐指数
2
解决办法
1276
查看次数

如何在方法impl的枚举中返回字符串中的&str?

我想返回城市/城镇/村庄的名称作为参考str.我可以在实现中指定生命周期,但也为枚举导致错误指定它,因为它没有声明引用.

enum CityType {
    City { name: String /* ... */ },
    Town { name: String /* ... */ },
    Village { name: String /* ... */ },
}

impl CityType {
    fn name(self) -> &str {
        match self {
            CityType::City { name } => &name,
            CityType::Town { name, .. } => &name,
            CityType::Village { name } => &name,
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

操场

rust borrow-checker

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

标签 统计

rust ×5

arm64 ×1

borrow-checker ×1

hex ×1

inline-assembly ×1

llvm ×1

rust-cargo ×1

serde ×1