Zig、C、Rust的Pk1

Zig、C、Rust的Pk1

github.com上看到"A basic comparitive analysis of C, C++, Rust, and Zig.":https://github.com/CoalNova/BasicCompare/tree/main

里边的代码是9个月之前的,用现在的zig 0.11.0 及0.12-dev都无法通过编译(具体为:zig-windows-x86_64-0.12.0-dev.2543+9eda6ccef),现把C、Zig、Rust代码down下来,做了简单修改,编译(Release)执行后,果然体验到了Zig的与众不同。(此处声明:原代码版权属于https://github.com/CoalNova)

代码编译方式

C: zig cc c_test.c -O2 -o c_test.exe

Rust:Cargo build --release

Zig:zig build-exe zig_test.zig -O ReleaseFast

代码的变化

C、Rust的基本没变。

Zig的变化(具体的编译器会提示):

1、个别变量由var 改为const.

2、注释部分改为: //var args = std.process.Args();

var args = try std.process.argsWithAllocator(allocator);

defer args.deinit();

3、几处 @intCast(u32,i) 变为@intCast(i)

@intToFloat改为@floatFromInt ,减少参数,并且单独声明了const量。

编译运行结果图

Rust

C

Zig

Zig属于水平式内存安全、编译型语言,目前仍在变动,但对C的无缝交互,内存安全方面的较小的学习曲线,真的让人很期待。

相关推荐
Bigger1 小时前
Tauri (26)——托盘图标总对不上系统主题?一行 Template Image 搞定
前端·rust·app
doiito7 小时前
【Agent Harness】TPS的“自工程完结”教会了我一件事:别把Bug留给下一道工序
架构·rust
doiito17 小时前
【Agent Harness】Gliding Horse 记忆系统深度剖析:像 CPU 一样思考的 AI 记忆架构
ai·rust·架构设计·系统设计·ai agent
doiito1 天前
【Agent Harness】Gliding Horse 给 Agent OS 装上双曲空间引擎与默克尔树边云同步
ai·rust·架构设计·系统设计·ai agent
doiito2 天前
【Agent Harness】Gliding Horse 本体论系统设计:给 AI Agent 装上“语义大脑”
ai·rust·架构设计·系统设计·ai agent
大卫小东(Sheldon)3 天前
Rust 推荐使用宏而非普通函数的场景
rust
doiito3 天前
【Agent Harness】为什么我把 JSON‑LD “编译成 DAG” 后,整个 Agent 平台立刻聪明了
ai·rust·架构设计·系统设计·ai agent
jump_jump4 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github