Rust のプレリュード (prelude) とは何か
use std::io::prelude::*;
というコードを見かけて、「プレリュード (prelude) ってなんだっけ」 と思ったので調べた。
Rust のプレリュード
まず、Rust で真にプレリュードと呼ばれるものは std::prelude
というモジュールらしい。
Rust comes with a variety of things in its standard library. However, if you had to manually import every single thing that you used, it would be very verbose. But importing a lot of things that a program never uses isn't good either. A balance needs to be struck.
The prelude is the list of things that Rust automatically imports into every Rust program. It's kept as small as possible, and is focused on things, particularly traits, which are used in almost every single Rust program.
std::prelude - Rust
std::prelude
モジュールに含まれるものは、すべての Rust プログラムで自動的にインポートされる。 このような仕組みになっているのは、よく使うものも全て手動でインポートする必要があるならばコードが煩くなってしまうため。
他のプレリュード
Rust の真のプレリュードは上に述べた通りだが、それ以外にも、複数の型を使用する際のインポートをより便利にするパターンとしてプレリュードの名が使われている。
std::prelude - RustOther preludes
Preludes can be seen as a pattern to make using multiple types more convenient. As such, you'll find other preludes in the standard library, such as
std::io::prelude
. Various libraries in the Rust ecosystem may also define their own preludes.The difference between 'the prelude' and these other preludes is that they are not automatically use'd, and must be imported manually. This is still easier than importing all of their constituent components.
例えば std::io::prelude
というモジュールは、IO 周りでよく使う型のインポートを楽にするためのモジュールである。 以下のようにして使われる。
use std::io::prelude::*;
真のプレリュードとは違い、自動的にインポートされるわけではなく、あくまで手動のインポートを楽にするためのパターンとのこと。 (なので、prelude
という名前じゃなくても挙動的には良さそうである。)