0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rust 超入門 → 関数

Posted at

関数

/// 関数の定義
fn 関数名 ( 変数 ) { 本体 }

/// 戻り値がある場合
fn 関数名 ( 変数 ) -> 戻り値データ型 { 本体 }

一般関数

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

fn test_function() {
    println!("test function.");
}

/// 実行結果:
/// main!
/// test function.

引数ある関数

fn main() {
    test_function(1, 2);
}

fn test_function(a: i32, b: i32) {
    println!("a+b={}", a + b );
}

/// 実行結果:
/// a+b=3

戻り値ある関数

fn main() {
    println!("1+2={}", test_function(1, 2));
}

fn test_function(a: i32, b: i32) -> i32{
    return a + b;
}

/// 実行結果:
/// 1+2=3

ちょっとプラス 関数内の関数

fn main() {
    fn test_function() -> i32{
       1 + 2  
    }
    println!("1+2={}", test_function());
}

///実行結果:
/// 1+2=3
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?