言語仕様 > 関数
code:rust
fn func(arg type) rettype
ret value
end
table:修飾子一覧 (記述優先順)
修飾子 役割 備考
pub public 関数 デフォルトは private
abs 抽象関数 特性体のメソッドのみ
async 非同期関数
get ゲッタ self 関数のみ
set セッタ self 関数のみ
メソッド (self 関数)
self は self Self の糖衣構文
code:ruby
impl
# self の所有権を奪う
fn func(self|)
end
fn func_with_args(&mut self| arg1, arg2)
end
end
getter / setter
get / set キーワードを用いる
メソッド名は設定するフィールド名と同様にする
Note: 関数として記述するのが直感的でない
code:rust
struct Struct
field str
impl
fn get field(&self|) str
println("getting field")
ret self.field
end
fn set field(&self| value str)
println("setting field")
self.field = value
end
end
end
マクロ関数 廃止
Note: マクロ関数必要?
静的変数で代用できるためマクロ変数は存在しない
マクロ関数は多用すべきでなく、主に std モジュール内で定義される
code:macros.ches
macro fn say_hello()
println("Hello")
end
ToDo: 可変長引数について (マクロ関数でのみ対応)
特殊関数 廃止
関数名の接頭辞 @
table:特殊関数の一覧
@entry() エントリポイント
@const() コンストラクタ
@dest() デストラクタ
コンストラクタでの automatic field initialization
auto キーワードを用いる
code:ruby
fn @const(&self| auto arg1 s32, arg2 s32)
self.arg2 = arg2 + 1
end
オーバーロード 廃止
code:rust
let mut arg = &""
fn func_b(&b mut str)
end
デフォルト引数
code:rs
fn f(a1 str = "", a2 str)
#
end
f(auto, "")