# std, primitives and preludes

⭐️ In Rust, language elements are implemented by not only `std` **library** crate but also **compiler** as well. Examples,

* [**Primitives**](https://doc.rust-lang.org/std/#primitives) : Defined by the compiler and methods are implemented by `std` library directly on primitives.
* [**Standard Macros**](https://doc.rust-lang.org/std/#macros) : Defined by both compiler and `std`

`std` library has been has divided into [**modules**](https://doc.rust-lang.org/std/#modules), according to the main areas each covered.

⭐️ While primitives are implemented by the **compiler**, the standard library implements **most useful methods** directly on the primitive types. But some **rarely useful language elements** of some primitives are stored on relevant `std` **modules**. This is why you can see `char`, `str` and integer types on both [primitives](https://doc.rust-lang.org/std/#primitives) and [`std` modules](https://doc.rust-lang.org/std/#modules).

## Primitives

```rust
// Primitives : Defined by the compiler and methods are directly implemented by std
bool, char, slice, str

i8, i16, i32, i64, i128, isize // i128 : nightly-only experimental API
u8, u16, u32, u64, u128, usize // u128 : nightly-only experimental API

f32, f64

array, tuple

pointer, fn, reference
```

## Standard Macros

```rust
// Standard Macros also defined by both compiler and std
print, println, eprint, eprintln
format, format_args
write, writeln

concat, concat_idents, stringify //concat_idents : nightly-only experimental API

include, include_bytes, include_str

assert, assert_eq, assert_ne
debug_assert, debug_assert_eq, debug_assert_ne

try, panic, compile_error, unreachable, unimplemented

file, line, column, module_path
env, option_env
cfg

select, thread_local //select : nightly-only experimental API

vec
```

## Std Modules

```rust
// std modules
char, str

i8, i16, i32, i64, i128, isize // i128 : nightly-only experimental API
u8, u16, u32 ,u64, u128, usize // u128 : nightly-only experimental API
f32, f64
num

vec, slice, hash, heap, collections // heap : nightly-only experimental API

string, ascii, fmt

default

marker, clone, convert, cmp, iter

ops, ffi

option, result, panic, error

io
fs, path
mem, thread, sync
process, env
net
time
os

ptr, boxed, borrow, cell, any, rc

prelude

intrinsics // intrinsics : nightly-only experimental API
raw // raw : nightly-only experimental API
```

> 🔎 When examine [Rust’s source code](https://github.com/rust-lang/rust), you can see that [`src` directory](https://github.com/rust-lang/rust/tree/master/src) is a **workspace**. Even though its having many library crates, by examine [root Cargo.toml](https://github.com/rust-lang/rust/blob/master/src/Cargo.toml) file you can easily identify that main crates are [**rustc**](https://github.com/rust-lang/rust/tree/master/src/rustc)(compiler) and [**libstd**](https://github.com/rust-lang/rust/tree/master/src/libstd) (std). In libstd/lib.rs std modules have been **re-exported** via `pub use` and the original location of most of std modules is [`src/libcore`](https://github.com/rust-lang/rust/tree/master/src/libcore).

**Few important** `std` **modules are,**

* `std::io` - Core **I/O** functionality&#x20;
* `std::fs` - **Filesystem** specific functionality
* `std::path` - **Cross platform path** specific functionality
* `std::env` - **Process’s environment** related functionality
* `std::mem` - **Memory** related functionality
* `std::net` - **TCP/UDP** communication
* `std::os` - **OS** specific functionality
* `std::thread` - Native **threads** specific functionality
* `std::collections` - Core **Collection types**

> 💯 Refer [Rust Standard Library Documentation](https://doc.rust-lang.org/std/) for more details.

## Preludes

Even though Rust `std` contains many modules, by default it doesn’t load each and every thing of std library on every rust program. Instead, it loads only the **smallest list of things which require for almost every single Rust program**. These are called [**preludes**](https://doc.rust-lang.org/std/prelude/). They import only,

```rust
// Reexported core operators
pub use marker::{Copy, Send, Sized, Sync};
pub use ops::{Drop, Fn, FnMut, FnOnce};

// Reexported functions
pub use mem::drop;

// Reexported types and traits
pub use boxed::Box;
pub use borrow::ToOwned;
pub use clone::Clone;
pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
pub use convert::{AsRef, AsMut, Into, From};
pub use default::Default;
pub use iter::{Iterator, Extend, IntoIterator};
pub use iter::{DoubleEndedIterator, ExactSizeIterator};
pub use option::Option::{self, Some, None};
pub use result::Result::{self, Ok, Err};
pub use slice::SliceConcatExt;
pub use string::{String, ToString};
pub use vec::Vec;
```

> Preludes have been imported explicitly on [`libstd/lib.rs`](https://github.com/rust-lang/rust/blob/master/src/libstd/lib.rs#L353) and the whole list can be seen on [`libstd/prelude/v1.rs`](https://github.com/rust-lang/rust/blob/master/src/libstd/prelude/v1.rs).

⭐️ So technically, Rust inserts,

* `extern crate std;` : into the **crate root of every crate**
* `use std::prelude::v1::*;` : into **every module**

  So you don’t need to import these each time.

The concept of preludes is quite common on Rust libraries. Even some modules inside `std` crate (ex.libstd/io) and many libraries (ex. [diesel](https://github.com/diesel-rs/diesel/blob/master/diesel/src/lib.rs#L151)) are having their own `prelude` modules.

⭐️ But **preludes** are used to **create a single place to import all important components** which are **required while using the library**. They **do not load automatically** unless you imported them manually. Only `std::prelude` imports automatically in every Rust programs.
