Zig (プログラミング言語) コード例

Zig (プログラミング言語)

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2024/02/16 07:19 UTC 版)

コード例

Hello world

// zig version 0.9.1
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}

ジェネリック連結リスト

// ジェネリックなLinkedList型

const std = @import("std");
const stdout = std.io.getStdOut().writer();

fn LinkedList(comptime T: type) type {
    return struct {
        const Self = @This();
        pub const Node = struct {
            next: ?*Node = null,
            data: T,
        };

        first: ?*Node = null,

        pub fn prepend(list: *Self, new_node: *Node) void {
            new_node.next = list.first;
            list.first = new_node;
        }
        pub fn format(
            list: Self,
            comptime fmt: []const u8,
            options: std.fmt.FormatOptions,
            out_stream: anytype,
        ) !void {
            try out_stream.writeAll("( ");
            var it = list.first;
            while (it) |node| : (it = node.next) {
                try std.fmt.formatType(node.data, fmt, options, out_stream, 1);
                try out_stream.writeAll(" ");
            }
            try out_stream.writeAll(" )");
        }
    };
}

pub fn main() !void {
    const ListU32 = LinkedList(u32);
    var list = ListU32{};
    var node1 = ListU32.Node{ .data = 1 };
    var node2 = ListU32.Node{ .data = 2 };
    var node3 = ListU32.Node{ .data = 3 };
    list.prepend(&node1);
    list.prepend(&node2);
    list.prepend(&node3);
    try stdout.print("{}\n", .{list});
    try stdout.print("{b}\n", .{list});
}
実行結果
( 3 2 1 ) 
( 11 10 1 )

このコードは、Zig言語を使用して単方向連結リスト(LinkedList)を定義し、それを使用して整数の単方向連結リストを作成し、標準出力に出力するプログラムである。

最初の2行では、stdパッケージからioモジュールをインポートし、標準出力ストリームを取得する。

次に、LinkedList関数が定義されている。この関数は、ジェネリックなLinkedList型を作成する。型Tを取り、LinkedList型を返す。このLinkedList型は、Nodeという構造体を含み、それぞれがデータを保持する。また、LinkedList型自体もprependとformatというメソッドを持つ。prependメソッドは、リストの先頭にノードを追加する。formatメソッドは、リストの内容を指定された書式でフォーマットして出力する。

main関数では、LinkedList関数を使用してListU32という型を作成し、整数のLinkedListを作成する。その後、3つのノードを作成し、prependメソッドを使用してリストに追加する。最後に、標準出力にリストを出力する。


注釈

  1. ^ フィールド間のパディングがゼロの構造体のこと。

出典

  1. ^ 出典URL: https://ziglang.org/download/#release-0.11.0, 題名: Release 0.11.0
  2. ^ Motroc, Gabriela (2017年10月31日). ““Zig has all the elegant simplicity of C, minus all the ways to shoot yourself in the foot””. JAXenter. 2021年1月1日閲覧。
  3. ^ Elizabeth, Jane (2017年10月19日). “Tired of C? New programming language Zig aims to be more pragmatic and readable”. JAXenter. 2021年1月1日閲覧。
  4. ^ Yegulalp, Serdar (2016年8月29日). “New challenger joins Rust to topple C language”. InfoWorld. 2021年1月1日閲覧。
  5. ^ IT之家 (2020年7月12日). “想替代 C 的 Zig 语言成立了基金会” (中国語). 新浪数码. 2021年1月1日閲覧。
  6. ^ The Zig Programming Language”. ziglang.org. 2021年1月1日閲覧。
  7. ^ Mozilla’s Observatory, the Zig programming language, and uSens’ VR/AR SDK—SD Times news digest: Aug. 29, 2016”. SD Times (2016年8月29日). 2021年1月1日閲覧。
  8. ^ Zig competes with C instead of depending on it”. ziglang.org. 2021年1月1日閲覧。
  9. ^ Kelley, Andrew (2018年1月24日). “Unsafe Zig is Safer than Unsafe Rust”. andrewkelley.me. 2021年1月1日閲覧。
  10. ^ Anderson, Tim (2020年4月24日). “Keen to go _ExtInt? LLVM Clang compiler adds support for custom width integers”. The Register. 2021年1月1日閲覧。
  11. ^ Documentation - The Zig Programming Language”. ziglang.org. 2021年1月1日閲覧。
  12. ^ Lewkowicz, Jakub (2020年4月14日). “SD Times news digest: C++20 concepts in Visual Studio 2010 version 16.3, Bootstrap to drop IE support, and Zig 0.60 released”. SD Times. 2021年1月1日閲覧。
  13. ^ Bill, Ginger (2019年5月13日). “A Reply to The Road to Zig 1.0”. gingerBill. 2021年1月1日閲覧。
  14. ^ a b ziglang/zig”. GitHub. 2021年1月1日閲覧。
  15. ^ Tier System”. ziglang.org. 2021年1月1日閲覧。
  16. ^ zig cc”. ziglang.org (2020年4月13日). 2021年1月1日閲覧。
  17. ^ Add support for `zig cc` as C compiler. #13757”. GitHub. 2021年1月1日閲覧。





英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  
  •  Zig (プログラミング言語)のページへのリンク

辞書ショートカット

すべての辞書の索引

「Zig (プログラミング言語)」の関連用語

Zig (プログラミング言語)のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Zig (プログラミング言語)のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
ウィキペディアウィキペディア
All text is available under the terms of the GNU Free Documentation License.
この記事は、ウィキペディアのZig (プログラミング言語) (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。 Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。

©2024 GRAS Group, Inc.RSS