我不会在初始化程序之后发布 Zig 和预期的 ','

问题描述 投票:0回答:1

要使

MyStruct
可供公开访问,您应该在定义结构时使用
pub
关键字。具体方法如下:

pub
修复
MyStruct
的代码:

pub fn main() void {
    const add = fn(a: i32, b: i32) i32 {
        return a + b;  // Semicolon added here
    };

    pub const MyStruct = struct {  // Struct is now public
        field1: i32,
        field2: i32,
    };

    // Example usage of MyStruct
    const instance = MyStruct{
        .field1 = 10,
        .field2 = 20,
    };

    const result = add(instance.field1, instance.field2);
}

说明:

  • pub const MyStruct
    pub
    关键字使
    MyStruct
    可以从其他模块/文件访问。
  • 结构体用法:定义结构体后,您可以创建它的实例并访问其字段。

现在,

MyStruct
是公开的,可以在项目的其他地方使用。

https://chatgpt.com/c/be9b787c-671d-4272-b7bc-c70780e0281d

如何使用代码?我不知道。

zig
1个回答
0
投票

这将添加两个整数,我10分钟前才开始看入门

const std = @import("std");

pub const MyStruct = struct {  // Struct is now public
        field1: i32,
        field2: i32,
};

// Example usage of MyStruct
const instance = MyStruct{
        .field1 = 10,
        .field2 = 20,
};

fn add(a: i32, b: i32) i32 {
        return a + b;  // Semicolon added here
}

pub fn main() void {
    var result: i32 = 0;
    result = add(instance.field1, instance.field2);
    std.debug.print("{}\n", .{result});
}
© www.soinside.com 2019 - 2024. All rights reserved.