LoginSignup
11
9

More than 5 years have passed since last update.

Rust入門でHello, World!を軽く読んでみた

Last updated at Posted at 2016-02-08

Rustという言語がすごいらしいので、ちょっと入門してみました。
といっても Hello, World! しか触っていないのですが。 (^_^;A

multirustで入れたのだけれど、少し古いバージョンが入ってしまいました。(^_^;A

$ rustc --version
rustc 1.4.0 (8ab8581f6 2015-10-27)

hello, World! からちょっとずつ見ていきます。

fn main() {
    println!("Hello");
}

まずは --pretty expanded オプションつきで見てみます。

$ rustc -Z unstable-options --pretty expanded hello.rs

#![feature(no_std, prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
fn main() {
    ::std::io::_print(::std::fmt::Arguments::new_v1({
                                                        static __STATIC_FMTSTR:
                                                               &'static [&'static str]
                                                               =
                                                            &["Hello\n"];
                                                        __STATIC_FMTSTR
                                                    },
                                                    &match () { () => [], }));
}

このタイミングでコンパイラプラグインを読み込んだりexportしているマクロを参照しているそうです。すでに println マクロは展開されていますね!

次に --pretty expanded,identified オプション付きでみてみます。

$ rustc -Z unstable-options --pretty expanded,identified hello.rs

#![feature(no_std, prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*; /* 2 */
#[macro_use]
extern crate std as std; /* 3 */
fn main() {
    ((::std::io::_print /* 9
         */)(((::std::fmt::Arguments::new_v1 /* 11
                  */)(({
                           static __STATIC_FMTSTR: &'static [&'static str] =
                               (&([("Hello\n" /* 24 */)] /* 23 */) /* 22 */);
                           /*
                           15
                           */
                           (__STATIC_FMTSTR /* 25 */)
                       } /* block 13 */ /* 12 */),
                      (&(match (() /* 28 */) {
                             () /* pat 29 */ => ([] /* 30 */),
                         } /* 27 */) /* 26 */)) /* 10 */)) /* 8 */);
} /* block 6 */ /* 4 */

src/librustc/session/config.rs を読むと、この数字はAST Nodeに割り当てられたIDを意味しているようです。

libsyntax/ast_map を読んでいくともっと理解できそうです。 o(^_^)o

なんだかあんまりに中身がない記事ですが、はじめたばかりなのでご容赦いただければと。。

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9