Unsafe関数の呼び出し
Unsafe関数の呼び出し
未定義の動作を回避するために満たす必要がある追加の前提条件がある関数またはメソッドは、unsafe
とマークできます。
Unsafe関数の書き方
未定義の動作を回避するために特定の条件が必要な場合は、独自の関数を unsafe
とマークできます。
Speaker Notes
This slide should take about 5 minutes.
Unsafe関数の呼び出し
get_unchecked
, like most _unchecked
functions, is unsafe, because it can create UB if the range is incorrect. abs
is unsafe for a different reason: it is an external function (FFI). Calling external functions is usually only a problem when those functions do things with pointers which might violate Rust’s memory model, but in general any C function might have undefined behaviour under any arbitrary circumstances.
この例の "C"
は ABI です(他の ABI も使用できます)。
Unsafe関数の書き方
実際には、swap
関数ではポインタは使用しません。これは参照を使用することで安全に実行できます。
アンセーフな関数内では、アンセーフなコードをunsafe
ブロックなしに記述することができます。これは #[deny(unsafe_op_in_unsafe_fn)]
で禁止できます。追加するとどうなるか見てみましょう。これは、今後の Rust エディションで変更される可能性があります。