core/
lib.rs

1//! # The Rust Core Library
2//!
3//! The Rust Core Library is the dependency-free[^free] foundation of [The
4//! Rust Standard Library](../std/index.html). It is the portable glue
5//! between the language and its libraries, defining the intrinsic and
6//! primitive building blocks of all Rust code. It links to no
7//! upstream libraries, no system libraries, and no libc.
8//!
9//! [^free]: Strictly speaking, there are some symbols which are needed but
10//!          they aren't always necessary.
11//!
12//! The core library is *minimal*: it isn't even aware of heap allocation,
13//! nor does it provide concurrency or I/O. These things require
14//! platform integration, and this library is platform-agnostic.
15//!
16//! # How to use the core library
17//!
18//! Please note that all of these details are currently not considered stable.
19//!
20// FIXME: Fill me in with more detail when the interface settles
21//! This library is built on the assumption of a few existing symbols:
22//!
23//! * `memcpy`, `memmove`, `memset`, `memcmp`, `bcmp`, `strlen` - These are core memory routines
24//!   which are generated by Rust codegen backends. Additionally, this library can make explicit
25//!   calls to `strlen`. Their signatures are the same as found in C, but there are extra
26//!   assumptions about their semantics: For `memcpy`, `memmove`, `memset`, `memcmp`, and `bcmp`, if
27//!   the `n` parameter is 0, the function is assumed to not be UB, even if the pointers are NULL or
28//!   dangling. (Note that making extra assumptions about these functions is common among compilers:
29//!   [clang](https://reviews.llvm.org/D86993) and [GCC](https://gcc.gnu.org/onlinedocs/gcc/Standards.html#C-Language) do the same.)
30//!   These functions are often provided by the system libc, but can also be provided by the
31//!   [compiler-builtins crate](https://crates.io/crates/compiler_builtins).
32//!   Note that the library does not guarantee that it will always make these assumptions, so Rust
33//!   user code directly calling the C functions should follow the C specification! The advice for
34//!   Rust user code is to call the functions provided by this library instead (such as
35//!   `ptr::copy`).
36//!
37//! * Panic handler - This function takes one argument, a `&panic::PanicInfo`. It is up to consumers of this core
38//!   library to define this panic function; it is only required to never
39//!   return. You should mark your implementation using `#[panic_handler]`.
40//!
41//! * `rust_eh_personality` - is used by the failure mechanisms of the
42//!    compiler. This is often mapped to GCC's personality function, but crates
43//!    which do not trigger a panic can be assured that this function is never
44//!    called. The `lang` attribute is called `eh_personality`.
45
46#![stable(feature = "core", since = "1.6.0")]
47#![doc(
48    html_playground_url = "https://play.rust-lang.org/",
49    issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
50    test(no_crate_inject, attr(deny(warnings))),
51    test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
52)]
53#![doc(rust_logo)]
54#![doc(cfg_hide(
55    no_fp_fmt_parse,
56    target_pointer_width = "16",
57    target_pointer_width = "32",
58    target_pointer_width = "64",
59    target_has_atomic = "8",
60    target_has_atomic = "16",
61    target_has_atomic = "32",
62    target_has_atomic = "64",
63    target_has_atomic = "ptr",
64    target_has_atomic_equal_alignment = "8",
65    target_has_atomic_equal_alignment = "16",
66    target_has_atomic_equal_alignment = "32",
67    target_has_atomic_equal_alignment = "64",
68    target_has_atomic_equal_alignment = "ptr",
69    target_has_atomic_load_store = "8",
70    target_has_atomic_load_store = "16",
71    target_has_atomic_load_store = "32",
72    target_has_atomic_load_store = "64",
73    target_has_atomic_load_store = "ptr",
74))]
75#![no_core]
76#![rustc_coherence_is_core]
77#![rustc_preserve_ub_checks]
78//
79// Lints:
80#![deny(rust_2021_incompatible_or_patterns)]
81#![deny(unsafe_op_in_unsafe_fn)]
82#![deny(fuzzy_provenance_casts)]
83#![warn(deprecated_in_future)]
84#![warn(missing_debug_implementations)]
85#![warn(missing_docs)]
86#![allow(explicit_outlives_requirements)]
87#![allow(incomplete_features)]
88#![warn(multiple_supertrait_upcastable)]
89#![allow(internal_features)]
90#![deny(ffi_unwind_calls)]
91#![warn(unreachable_pub)]
92// Do not check link redundancy on bootstrapping phase
93#![allow(rustdoc::redundant_explicit_links)]
94#![warn(rustdoc::unescaped_backticks)]
95//
96// Ferrocene addition: We removed the tidy directives for alphabetical ordering to reduce the number
97// of conflicts we have when merging main.
98//
99// Library features:
100// not-alphabetical-start
101#![cfg_attr(not(feature = "ferrocene_certified"), feature(array_ptr_get))]
102#![cfg_attr(not(feature = "ferrocene_certified"), feature(asm_experimental_arch))]
103#![cfg_attr(not(feature = "ferrocene_certified"), feature(bigint_helper_methods))]
104#![cfg_attr(not(feature = "ferrocene_certified"), feature(bstr))]
105#![cfg_attr(not(feature = "ferrocene_certified"), feature(bstr_internals))]
106#![cfg_attr(not(feature = "ferrocene_certified"), feature(cfg_select))]
107#![cfg_attr(not(feature = "ferrocene_certified"), feature(cfg_target_has_reliable_f16_f128))]
108#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_carrying_mul_add))]
109#![feature(const_destruct)]
110#![feature(const_eval_select)]
111#![feature(core_intrinsics)]
112#![feature(coverage_attribute)]
113#![cfg_attr(not(feature = "ferrocene_certified"), feature(disjoint_bitor))]
114#![feature(internal_impls_macro)]
115#![cfg_attr(not(feature = "ferrocene_certified"), feature(ip))]
116#![cfg_attr(not(feature = "ferrocene_certified"), feature(is_ascii_octdigit))]
117#![cfg_attr(not(feature = "ferrocene_certified"), feature(lazy_get))]
118#![cfg_attr(not(feature = "ferrocene_certified"), feature(link_cfg))]
119#![cfg_attr(not(feature = "ferrocene_certified"), feature(offset_of_enum))]
120#![cfg_attr(not(feature = "ferrocene_certified"), feature(panic_internals))]
121#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_alignment_type))]
122#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_metadata))]
123#![cfg_attr(not(feature = "ferrocene_certified"), feature(set_ptr_value))]
124#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_as_array))]
125#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_ptr_get))]
126#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_internals))]
127#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_split_inclusive_remainder))]
128#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_split_remainder))]
129#![feature(ub_checks)]
130#![cfg_attr(not(feature = "ferrocene_certified"), feature(unchecked_neg))]
131#![cfg_attr(not(feature = "ferrocene_certified"), feature(unchecked_shifts))]
132#![cfg_attr(not(feature = "ferrocene_certified"), feature(unsafe_pinned))]
133#![cfg_attr(not(feature = "ferrocene_certified"), feature(utf16_extra))]
134#![cfg_attr(not(feature = "ferrocene_certified"), feature(variant_count))]
135// not-alphabetical-end
136//
137// Language features:
138// not-alphabetical-start
139#![cfg_attr(not(feature = "ferrocene_certified"), feature(abi_unadjusted))]
140#![feature(adt_const_params)]
141#![feature(allow_internal_unsafe)]
142#![feature(allow_internal_unstable)]
143#![feature(auto_traits)]
144#![cfg_attr(not(feature = "ferrocene_certified"), feature(cfg_sanitize))]
145#![feature(cfg_target_has_atomic)]
146#![feature(cfg_target_has_atomic_equal_alignment)]
147#![feature(cfg_ub_checks)]
148#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_precise_live_drops))]
149#![feature(const_trait_impl)]
150#![feature(decl_macro)]
151#![cfg_attr(not(feature = "ferrocene_certified"), feature(deprecated_suggestion))]
152#![cfg_attr(not(feature = "ferrocene_certified"), feature(doc_cfg))]
153#![feature(doc_cfg_hide)]
154#![cfg_attr(not(feature = "ferrocene_certified"), feature(doc_notable_trait))]
155#![cfg_attr(not(feature = "ferrocene_certified"), feature(extern_types))]
156#![cfg_attr(not(feature = "ferrocene_certified"), feature(f128))]
157#![cfg_attr(not(feature = "ferrocene_certified"), feature(f16))]
158#![feature(freeze_impls)]
159#![feature(fundamental)]
160#![cfg_attr(not(feature = "ferrocene_certified"), feature(if_let_guard))]
161#![feature(intra_doc_pointers)]
162#![feature(intrinsics)]
163#![feature(lang_items)]
164#![cfg_attr(not(feature = "ferrocene_certified"), feature(link_llvm_intrinsics))]
165#![cfg_attr(not(feature = "ferrocene_certified"), feature(macro_metavar_expr))]
166#![cfg_attr(not(feature = "ferrocene_certified"), feature(macro_metavar_expr_concat))]
167#![cfg_attr(not(feature = "ferrocene_certified"), feature(marker_trait_attr))]
168#![cfg_attr(not(feature = "ferrocene_certified"), feature(min_specialization))]
169#![feature(multiple_supertrait_upcastable)]
170#![cfg_attr(not(feature = "ferrocene_certified"), feature(must_not_suspend))]
171#![feature(negative_impls)]
172#![feature(never_type)]
173#![feature(no_core)]
174#![feature(optimize_attribute)]
175#![feature(prelude_import)]
176#![cfg_attr(not(feature = "ferrocene_certified"), feature(repr_simd))]
177#![feature(rustc_allow_const_fn_unstable)]
178#![feature(rustc_attrs)]
179#![feature(rustdoc_internals)]
180#![cfg_attr(not(feature = "ferrocene_certified"), feature(simd_ffi))]
181#![feature(staged_api)]
182#![cfg_attr(not(feature = "ferrocene_certified"), feature(stmt_expr_attributes))]
183#![feature(strict_provenance_lints)]
184#![feature(trait_alias)]
185#![cfg_attr(not(feature = "ferrocene_certified"), feature(transparent_unions))]
186#![feature(try_blocks)]
187#![feature(unboxed_closures)]
188#![cfg_attr(not(feature = "ferrocene_certified"), feature(unsized_fn_params))]
189#![cfg_attr(not(feature = "ferrocene_certified"), feature(with_negative_coherence))]
190// not-alphabetical-end
191//
192// Target features:
193// not-alphabetical-start
194#![cfg_attr(not(feature = "ferrocene_certified"), feature(aarch64_unstable_target_feature))]
195#![cfg_attr(not(feature = "ferrocene_certified"), feature(arm_target_feature))]
196#![cfg_attr(not(feature = "ferrocene_certified"), feature(hexagon_target_feature))]
197#![cfg_attr(not(feature = "ferrocene_certified"), feature(loongarch_target_feature))]
198#![cfg_attr(not(feature = "ferrocene_certified"), feature(mips_target_feature))]
199#![cfg_attr(not(feature = "ferrocene_certified"), feature(powerpc_target_feature))]
200#![cfg_attr(not(feature = "ferrocene_certified"), feature(riscv_target_feature))]
201#![cfg_attr(not(feature = "ferrocene_certified"), feature(rtm_target_feature))]
202#![cfg_attr(not(feature = "ferrocene_certified"), feature(s390x_target_feature))]
203#![cfg_attr(not(feature = "ferrocene_certified"), feature(sse4a_target_feature))]
204#![cfg_attr(not(feature = "ferrocene_certified"), feature(tbm_target_feature))]
205#![cfg_attr(not(feature = "ferrocene_certified"), feature(wasm_target_feature))]
206#![cfg_attr(not(feature = "ferrocene_certified"), feature(x86_amx_intrinsics))]
207// not-alphabetical-end
208//
209// Ferrocene lints/features:
210#![allow(unused_attributes)]
211#![cfg_attr(feature = "ferrocene_certified", allow(rustdoc::broken_intra_doc_links))]
212
213// allow using `core::` in intra-doc links
214#[allow(unused_extern_crates)]
215extern crate self as core;
216
217#[prelude_import]
218#[allow(unused)]
219use prelude::rust_2024::*;
220
221#[macro_use]
222mod macros;
223
224#[unstable(feature = "assert_matches", issue = "82775")]
225/// Unstable module containing the unstable `assert_matches` macro.
226#[cfg(not(feature = "ferrocene_certified"))]
227pub mod assert_matches {
228    #[unstable(feature = "assert_matches", issue = "82775")]
229    pub use crate::macros::{assert_matches, debug_assert_matches};
230}
231
232// We don't export this through #[macro_export] for now, to avoid breakage.
233#[unstable(feature = "autodiff", issue = "124509")]
234/// Unstable module containing the unstable `autodiff` macro.
235pub mod autodiff {
236    #[unstable(feature = "autodiff", issue = "124509")]
237    pub use crate::macros::builtin::{autodiff_forward, autodiff_reverse};
238}
239
240#[unstable(feature = "contracts", issue = "128044")]
241#[cfg(not(feature = "ferrocene_certified"))]
242pub mod contracts;
243
244#[unstable(feature = "cfg_select", issue = "115585")]
245#[cfg(not(feature = "ferrocene_certified"))]
246pub use crate::macros::cfg_select;
247
248#[macro_use]
249mod internal_macros;
250
251#[path = "num/shells/int_macros.rs"]
252#[macro_use]
253#[cfg(not(feature = "ferrocene_certified"))]
254mod int_macros;
255
256#[rustc_diagnostic_item = "i128_legacy_mod"]
257#[path = "num/shells/i128.rs"]
258#[cfg(not(feature = "ferrocene_certified"))]
259pub mod i128;
260#[rustc_diagnostic_item = "i16_legacy_mod"]
261#[path = "num/shells/i16.rs"]
262#[cfg(not(feature = "ferrocene_certified"))]
263pub mod i16;
264#[rustc_diagnostic_item = "i32_legacy_mod"]
265#[path = "num/shells/i32.rs"]
266#[cfg(not(feature = "ferrocene_certified"))]
267pub mod i32;
268#[rustc_diagnostic_item = "i64_legacy_mod"]
269#[path = "num/shells/i64.rs"]
270#[cfg(not(feature = "ferrocene_certified"))]
271pub mod i64;
272#[rustc_diagnostic_item = "i8_legacy_mod"]
273#[path = "num/shells/i8.rs"]
274#[cfg(not(feature = "ferrocene_certified"))]
275pub mod i8;
276#[rustc_diagnostic_item = "isize_legacy_mod"]
277#[path = "num/shells/isize.rs"]
278#[cfg(not(feature = "ferrocene_certified"))]
279pub mod isize;
280
281#[rustc_diagnostic_item = "u128_legacy_mod"]
282#[path = "num/shells/u128.rs"]
283#[cfg(not(feature = "ferrocene_certified"))]
284pub mod u128;
285#[rustc_diagnostic_item = "u16_legacy_mod"]
286#[path = "num/shells/u16.rs"]
287#[cfg(not(feature = "ferrocene_certified"))]
288pub mod u16;
289#[rustc_diagnostic_item = "u32_legacy_mod"]
290#[path = "num/shells/u32.rs"]
291#[cfg(not(feature = "ferrocene_certified"))]
292pub mod u32;
293#[rustc_diagnostic_item = "u64_legacy_mod"]
294#[path = "num/shells/u64.rs"]
295#[cfg(not(feature = "ferrocene_certified"))]
296pub mod u64;
297#[rustc_diagnostic_item = "u8_legacy_mod"]
298#[path = "num/shells/u8.rs"]
299#[cfg(not(feature = "ferrocene_certified"))]
300pub mod u8;
301#[rustc_diagnostic_item = "usize_legacy_mod"]
302#[path = "num/shells/usize.rs"]
303#[cfg(not(feature = "ferrocene_certified"))]
304pub mod usize;
305
306#[path = "num/f128.rs"]
307#[cfg(not(feature = "ferrocene_certified"))]
308pub mod f128;
309#[path = "num/f16.rs"]
310#[cfg(not(feature = "ferrocene_certified"))]
311pub mod f16;
312#[path = "num/f32.rs"]
313#[cfg(not(feature = "ferrocene_certified"))]
314pub mod f32;
315#[path = "num/f64.rs"]
316#[cfg(not(feature = "ferrocene_certified"))]
317pub mod f64;
318
319#[macro_use]
320pub mod num;
321
322/* The core prelude, not as all-encompassing as the std prelude */
323
324pub mod prelude;
325
326/* Core modules for ownership management */
327
328pub mod hint;
329pub mod intrinsics;
330pub mod mem;
331pub mod ptr;
332#[unstable(feature = "ub_checks", issue = "none")]
333pub mod ub_checks;
334
335/* Core language traits */
336
337#[cfg(not(feature = "ferrocene_certified"))]
338pub mod borrow;
339pub mod clone;
340pub mod cmp;
341pub mod convert;
342pub mod default;
343#[cfg(not(feature = "ferrocene_certified"))]
344pub mod error;
345pub mod marker;
346pub mod ops;
347
348/* Core types and methods on primitives */
349
350#[cfg(not(feature = "ferrocene_certified"))]
351pub mod any;
352pub mod array;
353#[cfg(not(feature = "ferrocene_certified"))]
354pub mod ascii;
355#[cfg(not(feature = "ferrocene_certified"))]
356pub mod asserting;
357#[unstable(feature = "async_iterator", issue = "79024")]
358#[cfg(not(feature = "ferrocene_certified"))]
359pub mod async_iter;
360#[unstable(feature = "bstr", issue = "134915")]
361#[cfg(not(feature = "ferrocene_certified"))]
362pub mod bstr;
363pub mod cell;
364#[cfg(not(feature = "ferrocene_certified"))]
365pub mod char;
366#[cfg(not(feature = "ferrocene_certified"))]
367pub mod ffi;
368#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
369#[cfg(not(feature = "ferrocene_certified"))]
370pub mod io;
371pub mod iter;
372#[cfg(not(feature = "ferrocene_certified"))]
373pub mod net;
374pub mod option;
375pub mod panic;
376pub mod panicking;
377#[unstable(feature = "pattern_type_macro", issue = "123646")]
378#[cfg(not(feature = "ferrocene_certified"))]
379pub mod pat;
380#[cfg(not(feature = "ferrocene_certified"))]
381pub mod pin;
382#[unstable(feature = "random", issue = "130703")]
383#[cfg(not(feature = "ferrocene_certified"))]
384pub mod random;
385#[unstable(feature = "new_range_api", issue = "125687")]
386#[cfg(not(feature = "ferrocene_certified"))]
387pub mod range;
388pub mod result;
389pub mod sync;
390#[unstable(feature = "unsafe_binders", issue = "130516")]
391#[cfg(not(feature = "ferrocene_certified"))]
392pub mod unsafe_binder;
393
394pub mod fmt;
395#[cfg(not(feature = "ferrocene_certified"))]
396pub mod hash;
397#[cfg(not(feature = "ferrocene_certified"))]
398pub mod slice;
399#[cfg(not(feature = "ferrocene_certified"))]
400pub mod str;
401#[cfg(not(feature = "ferrocene_certified"))]
402pub mod time;
403
404#[cfg(not(feature = "ferrocene_certified"))]
405pub mod unicode;
406
407/* Async */
408#[cfg(not(feature = "ferrocene_certified"))]
409pub mod future;
410#[cfg(not(feature = "ferrocene_certified"))]
411pub mod task;
412
413/* Heap memory allocator trait */
414#[allow(missing_docs)]
415pub mod alloc;
416
417// note: does not need to be public
418mod bool;
419#[cfg(not(feature = "ferrocene_certified"))]
420mod escape;
421#[cfg(not(feature = "ferrocene_certified"))]
422mod tuple;
423#[cfg(not(feature = "ferrocene_certified"))]
424mod unit;
425
426#[stable(feature = "core_primitive", since = "1.43.0")]
427#[cfg(not(feature = "ferrocene_certified"))]
428pub mod primitive;
429
430// Pull in the `core_arch` crate directly into core. The contents of
431// `core_arch` are in a different repository: rust-lang/stdarch.
432//
433// `core_arch` depends on core, but the contents of this module are
434// set up in such a way that directly pulling it here works such that the
435// crate uses the this crate as its core.
436#[path = "../../stdarch/crates/core_arch/src/mod.rs"]
437#[allow(
438    missing_docs,
439    missing_debug_implementations,
440    dead_code,
441    unused_imports,
442    unsafe_op_in_unsafe_fn,
443    ambiguous_glob_reexports,
444    deprecated_in_future,
445    unreachable_pub
446)]
447#[allow(rustdoc::bare_urls)]
448#[cfg(not(feature = "ferrocene_certified"))]
449mod core_arch;
450
451#[stable(feature = "simd_arch", since = "1.27.0")]
452#[cfg(not(feature = "ferrocene_certified"))]
453pub mod arch;
454
455// Pull in the `core_simd` crate directly into core. The contents of
456// `core_simd` are in a different repository: rust-lang/portable-simd.
457//
458// `core_simd` depends on core, but the contents of this module are
459// set up in such a way that directly pulling it here works such that the
460// crate uses this crate as its core.
461#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
462#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
463#[allow(rustdoc::bare_urls)]
464#[unstable(feature = "portable_simd", issue = "86656")]
465#[cfg(not(feature = "ferrocene_certified"))]
466mod core_simd;
467
468#[unstable(feature = "portable_simd", issue = "86656")]
469#[cfg(not(feature = "ferrocene_certified"))]
470pub mod simd {
471    #![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
472
473    #[unstable(feature = "portable_simd", issue = "86656")]
474    pub use crate::core_simd::simd::*;
475}
476
477include!("primitive_docs.rs");