2
1

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 5 years have passed since last update.

Rustと関数実装

2
Last updated at Posted at 2021-03-25

関数表記

  • Rustでは、関数と変数の命名規則は、スネークケースを使うのが慣例のようです。
  • また、関数定義は、fnで始まります。

例:

fn main() {
    another_function();
}

fn another_function() {
    println!("Another function.");
}
  • Rustにおける関数の引数を利用したパターンは下記のように書くことができます。
  • 注意点としては、関数は各仮引数の型を宣言しなければなりません。=> (x: i32)

例:

fn main() {
    another_function(5);
}

fn another_function(x: i32) {
    println!("value = {}", x);
}

[実行結果]

❯ cargo run
   Compiling variables v0.1.0 (/Users/yoshitaka.koitabashi/Desktop/variables)
    Finished dev [unoptimized + debuginfo] target(s) in 0.24s
     Running `target/debug/variables`
value = 5
  • Rustでは、関数の最後に式を置くか文を置くかどちらかの形で形成されます。

ちなみに、
文とは、なんらかの動作をして値を返さない命令です。
式とは、結果値に評価されます。

  • 下記の例は、letを使用し変数を生成して、値を代入すること => 文

例:

fn main() {
    let y = 6;
}
  • 下記の例は、エラーになります。
  • 理由は、(let y = 6)は文であり値を返さないので、let文自体を他の変数に代入することはできません。

例:

fn main() {
    let x = (let y = 6);
}
  • 下記は、初めに(let x = 5;)の文宣言していますが、その後(let x = 3;)で再度宣言し直しています。
  • その後、(x + 1)で評価を行っているので、yは4に評価されます。
  • 注意したいのは、(x + 1)で文末にセミコロンがついていないません。
  • 理由は、式は終端にセミコロンを含みません。
  • 式の終端にセミコロンを付けたら、文に変わってしまうためです。

例:

fn main() {
    let x = 5;
    let y = {
        let x = 3;
        x + 1
    };

    println!("value = {}", y);
}
  • 戻り値のある関数については、**->**の後に型を書いて宣言できます。
  • 下記の例を示します。
  • let x = five()は、関数の戻り値を使って変数を初期化しています。
  • five()関数は、仮引数をもたず戻り値の型を定義しています。

例:

fn five() -> i32 {
    5
}

fn main() {
    let x = five();

    println!("value = {}", x);
}

[実行結果]

❯ cargo run
   Compiling variables v0.1.0 (/Users/yoshitaka.koitabashi/Desktop/variables)
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/variables`
value = 5
  • 関数に戻り値の型が指定されていない場合、unitと呼ばれる空のタプルを返します。

例:

fn make_nothing() -> () {
    return ();
}

fn make_nothing2() {
}

fn main() {
    let a = make_nothing();
    let b = make_nothing2();

    println!("a: {:?}", a);
    println!("b: {:?}", b);
}

[実行結果]

❯ cargo run
   Compiling variables v0.1.0 (/Users/yoshitaka.koitabashi/Desktop/variables)
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/variables`
a: ()
b: ()

応用

  • 下記の例はどうでしょうか。
  • println!("{} {}", result.0, result.1);では、 戻り値をタプルで返しています。
  • println!("{} {}", a, b);では、タプルを2つの変数に分解しています。

例:

fn swap(x: i32, y: i32) -> (i32, i32) {
    return (y, x);
}

fn main() {
    let result = swap(123, 321);
    println!("{} {}", result.0, result.1);

    let (a, b) = swap(result.0, result.1);
    println!("{} {}", a, b);
}

[実行結果]

❯ cargo run
   Compiling variables v0.1.0 (/Users/yoshitaka.koitabashi/Desktop/variables)
    Finished dev [unoptimized + debuginfo] target(s) in 0.82s
     Running `target/debug/variables`
321 123
123 321

参考文献

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?