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#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_cmp))]
110#![feature(const_destruct)]
111#![feature(const_eval_select)]
112#![feature(core_intrinsics)]
113#![feature(coverage_attribute)]
114#![cfg_attr(not(feature = "ferrocene_certified"), feature(disjoint_bitor))]
115#![feature(internal_impls_macro)]
116#![cfg_attr(not(feature = "ferrocene_certified"), feature(ip))]
117#![cfg_attr(not(feature = "ferrocene_certified"), feature(is_ascii_octdigit))]
118#![cfg_attr(not(feature = "ferrocene_certified"), feature(lazy_get))]
119#![cfg_attr(not(feature = "ferrocene_certified"), feature(link_cfg))]
120#![cfg_attr(not(feature = "ferrocene_certified"), feature(offset_of_enum))]
121#![cfg_attr(not(feature = "ferrocene_certified"), feature(panic_internals))]
122#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_alignment_type))]
123#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_metadata))]
124#![cfg_attr(not(feature = "ferrocene_certified"), feature(set_ptr_value))]
125#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_as_array))]
126#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_ptr_get))]
127#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_internals))]
128#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_split_inclusive_remainder))]
129#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_split_remainder))]
130#![feature(ub_checks)]
131#![cfg_attr(not(feature = "ferrocene_certified"), feature(unchecked_neg))]
132#![cfg_attr(not(feature = "ferrocene_certified"), feature(unchecked_shifts))]
133#![cfg_attr(not(feature = "ferrocene_certified"), feature(unsafe_pinned))]
134#![cfg_attr(not(feature = "ferrocene_certified"), feature(utf16_extra))]
135#![cfg_attr(not(feature = "ferrocene_certified"), feature(variant_count))]
136// not-alphabetical-end
137//
138// Language features:
139// not-alphabetical-start
140#![cfg_attr(not(feature = "ferrocene_certified"), feature(abi_unadjusted))]
141#![feature(adt_const_params)]
142#![feature(allow_internal_unsafe)]
143#![feature(allow_internal_unstable)]
144#![feature(auto_traits)]
145#![cfg_attr(not(feature = "ferrocene_certified"), feature(cfg_sanitize))]
146#![feature(cfg_target_has_atomic)]
147#![feature(cfg_target_has_atomic_equal_alignment)]
148#![feature(cfg_ub_checks)]
149#![feature(const_precise_live_drops)]
150#![feature(const_trait_impl)]
151#![feature(decl_macro)]
152#![cfg_attr(not(feature = "ferrocene_certified"), feature(deprecated_suggestion))]
153#![cfg_attr(not(feature = "ferrocene_certified"), feature(doc_cfg))]
154#![cfg_attr(not(feature = "ferrocene_certified"), feature(derive_const))]
155#![feature(doc_cfg_hide)]
156#![cfg_attr(not(feature = "ferrocene_certified"), feature(doc_notable_trait))]
157#![cfg_attr(not(feature = "ferrocene_certified"), feature(extern_types))]
158#![cfg_attr(not(feature = "ferrocene_certified"), feature(f128))]
159#![cfg_attr(not(feature = "ferrocene_certified"), feature(f16))]
160#![feature(freeze_impls)]
161#![feature(fundamental)]
162#![cfg_attr(not(feature = "ferrocene_certified"), feature(if_let_guard))]
163#![feature(intra_doc_pointers)]
164#![feature(intrinsics)]
165#![feature(lang_items)]
166#![cfg_attr(not(feature = "ferrocene_certified"), feature(link_llvm_intrinsics))]
167#![cfg_attr(not(feature = "ferrocene_certified"), feature(macro_metavar_expr))]
168#![cfg_attr(not(feature = "ferrocene_certified"), feature(macro_metavar_expr_concat))]
169#![cfg_attr(not(feature = "ferrocene_certified"), feature(marker_trait_attr))]
170#![cfg_attr(not(feature = "ferrocene_certified"), feature(min_specialization))]
171#![feature(multiple_supertrait_upcastable)]
172#![cfg_attr(not(feature = "ferrocene_certified"), feature(must_not_suspend))]
173#![feature(negative_impls)]
174#![feature(never_type)]
175#![feature(no_core)]
176#![feature(optimize_attribute)]
177#![feature(prelude_import)]
178#![feature(reborrow)]
179#![cfg_attr(not(feature = "ferrocene_certified"), feature(repr_simd))]
180#![feature(rustc_allow_const_fn_unstable)]
181#![feature(rustc_attrs)]
182#![feature(rustdoc_internals)]
183#![cfg_attr(not(feature = "ferrocene_certified"), feature(simd_ffi))]
184#![feature(staged_api)]
185#![cfg_attr(not(feature = "ferrocene_certified"), feature(stmt_expr_attributes))]
186#![feature(strict_provenance_lints)]
187#![feature(trait_alias)]
188#![cfg_attr(not(feature = "ferrocene_certified"), feature(transparent_unions))]
189#![feature(try_blocks)]
190#![feature(unboxed_closures)]
191#![cfg_attr(not(feature = "ferrocene_certified"), feature(unsized_fn_params))]
192#![cfg_attr(not(feature = "ferrocene_certified"), feature(with_negative_coherence))]
193// not-alphabetical-end
194//
195// Target features:
196// not-alphabetical-start
197#![cfg_attr(not(feature = "ferrocene_certified"), feature(aarch64_unstable_target_feature))]
198#![cfg_attr(not(feature = "ferrocene_certified"), feature(arm_target_feature))]
199#![cfg_attr(not(feature = "ferrocene_certified"), feature(hexagon_target_feature))]
200#![cfg_attr(not(feature = "ferrocene_certified"), feature(loongarch_target_feature))]
201#![cfg_attr(not(feature = "ferrocene_certified"), feature(mips_target_feature))]
202#![cfg_attr(not(feature = "ferrocene_certified"), feature(powerpc_target_feature))]
203#![cfg_attr(not(feature = "ferrocene_certified"), feature(nvptx_target_feature))]
204#![cfg_attr(not(feature = "ferrocene_certified"), feature(riscv_target_feature))]
205#![cfg_attr(not(feature = "ferrocene_certified"), feature(rtm_target_feature))]
206#![cfg_attr(not(feature = "ferrocene_certified"), feature(s390x_target_feature))]
207#![cfg_attr(not(feature = "ferrocene_certified"), feature(wasm_target_feature))]
208#![cfg_attr(not(feature = "ferrocene_certified"), feature(x86_amx_intrinsics))]
209// not-alphabetical-end
210//
211// Ferrocene lints/features:
212#![allow(unused_attributes)]
213#![cfg_attr(feature = "ferrocene_certified", allow(rustdoc::broken_intra_doc_links))]
214
215// allow using `core::` in intra-doc links
216#[allow(unused_extern_crates)]
217extern crate self as core;
218
219/* The core prelude, not as all-encompassing as the std prelude */
220// The compiler expects the prelude definition to be defined before it's use statement.
221pub mod prelude;
222
223#[prelude_import]
224#[allow(unused)]
225use prelude::rust_2024::*;
226
227#[macro_use]
228mod macros;
229
230#[unstable(feature = "assert_matches", issue = "82775")]
231/// Unstable module containing the unstable `assert_matches` macro.
232#[cfg(not(feature = "ferrocene_certified"))]
233pub mod assert_matches {
234    #[unstable(feature = "assert_matches", issue = "82775")]
235    pub use crate::macros::{assert_matches, debug_assert_matches};
236}
237
238#[unstable(feature = "derive_from", issue = "144889")]
239/// Unstable module containing the unstable `From` derive macro.
240pub mod from {
241    #[unstable(feature = "derive_from", issue = "144889")]
242    pub use crate::macros::builtin::From;
243}
244
245// We don't export this through #[macro_export] for now, to avoid breakage.
246#[unstable(feature = "autodiff", issue = "124509")]
247/// Unstable module containing the unstable `autodiff` macro.
248pub mod autodiff {
249    #[unstable(feature = "autodiff", issue = "124509")]
250    pub use crate::macros::builtin::{autodiff_forward, autodiff_reverse};
251}
252
253#[unstable(feature = "contracts", issue = "128044")]
254#[cfg(not(feature = "ferrocene_certified"))]
255pub mod contracts;
256
257#[unstable(feature = "cfg_select", issue = "115585")]
258#[cfg(not(feature = "ferrocene_certified"))]
259pub use crate::macros::cfg_select;
260
261#[macro_use]
262mod internal_macros;
263
264#[path = "num/shells/int_macros.rs"]
265#[macro_use]
266#[cfg(not(feature = "ferrocene_certified"))]
267mod int_macros;
268
269#[rustc_diagnostic_item = "i128_legacy_mod"]
270#[path = "num/shells/i128.rs"]
271#[cfg(not(feature = "ferrocene_certified"))]
272pub mod i128;
273#[rustc_diagnostic_item = "i16_legacy_mod"]
274#[path = "num/shells/i16.rs"]
275#[cfg(not(feature = "ferrocene_certified"))]
276pub mod i16;
277#[rustc_diagnostic_item = "i32_legacy_mod"]
278#[path = "num/shells/i32.rs"]
279#[cfg(not(feature = "ferrocene_certified"))]
280pub mod i32;
281#[rustc_diagnostic_item = "i64_legacy_mod"]
282#[path = "num/shells/i64.rs"]
283#[cfg(not(feature = "ferrocene_certified"))]
284pub mod i64;
285#[rustc_diagnostic_item = "i8_legacy_mod"]
286#[path = "num/shells/i8.rs"]
287#[cfg(not(feature = "ferrocene_certified"))]
288pub mod i8;
289#[rustc_diagnostic_item = "isize_legacy_mod"]
290#[path = "num/shells/isize.rs"]
291#[cfg(not(feature = "ferrocene_certified"))]
292pub mod isize;
293
294#[rustc_diagnostic_item = "u128_legacy_mod"]
295#[path = "num/shells/u128.rs"]
296#[cfg(not(feature = "ferrocene_certified"))]
297pub mod u128;
298#[rustc_diagnostic_item = "u16_legacy_mod"]
299#[path = "num/shells/u16.rs"]
300#[cfg(not(feature = "ferrocene_certified"))]
301pub mod u16;
302#[rustc_diagnostic_item = "u32_legacy_mod"]
303#[path = "num/shells/u32.rs"]
304#[cfg(not(feature = "ferrocene_certified"))]
305pub mod u32;
306#[rustc_diagnostic_item = "u64_legacy_mod"]
307#[path = "num/shells/u64.rs"]
308#[cfg(not(feature = "ferrocene_certified"))]
309pub mod u64;
310#[rustc_diagnostic_item = "u8_legacy_mod"]
311#[path = "num/shells/u8.rs"]
312#[cfg(not(feature = "ferrocene_certified"))]
313pub mod u8;
314#[rustc_diagnostic_item = "usize_legacy_mod"]
315#[path = "num/shells/usize.rs"]
316#[cfg(not(feature = "ferrocene_certified"))]
317pub mod usize;
318
319#[path = "num/f128.rs"]
320#[cfg(not(feature = "ferrocene_certified"))]
321pub mod f128;
322#[path = "num/f16.rs"]
323#[cfg(not(feature = "ferrocene_certified"))]
324pub mod f16;
325#[path = "num/f32.rs"]
326#[cfg(not(feature = "ferrocene_certified"))]
327pub mod f32;
328#[path = "num/f64.rs"]
329#[cfg(not(feature = "ferrocene_certified"))]
330pub mod f64;
331
332#[macro_use]
333pub mod num;
334
335/* Core modules for ownership management */
336
337pub mod hint;
338pub mod intrinsics;
339pub mod mem;
340pub mod ptr;
341#[unstable(feature = "ub_checks", issue = "none")]
342pub mod ub_checks;
343
344/* Core language traits */
345
346#[cfg(not(feature = "ferrocene_certified"))]
347pub mod borrow;
348pub mod clone;
349pub mod cmp;
350pub mod convert;
351pub mod default;
352#[cfg(not(feature = "ferrocene_certified"))]
353pub mod error;
354pub mod marker;
355pub mod ops;
356
357/* Core types and methods on primitives */
358
359#[cfg(not(feature = "ferrocene_certified"))]
360pub mod any;
361pub mod array;
362#[cfg(not(feature = "ferrocene_certified"))]
363pub mod ascii;
364#[cfg(not(feature = "ferrocene_certified"))]
365pub mod asserting;
366#[unstable(feature = "async_iterator", issue = "79024")]
367#[cfg(not(feature = "ferrocene_certified"))]
368pub mod async_iter;
369#[unstable(feature = "bstr", issue = "134915")]
370#[cfg(not(feature = "ferrocene_certified"))]
371pub mod bstr;
372pub mod cell;
373#[cfg(not(feature = "ferrocene_certified"))]
374pub mod char;
375#[cfg(not(feature = "ferrocene_certified"))]
376pub mod ffi;
377#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
378#[cfg(not(feature = "ferrocene_certified"))]
379pub mod io;
380pub mod iter;
381#[cfg(not(feature = "ferrocene_certified"))]
382pub mod net;
383pub mod option;
384pub mod panic;
385pub mod panicking;
386#[unstable(feature = "pattern_type_macro", issue = "123646")]
387#[cfg(not(feature = "ferrocene_certified"))]
388pub mod pat;
389#[cfg(not(feature = "ferrocene_certified"))]
390pub mod pin;
391#[unstable(feature = "random", issue = "130703")]
392#[cfg(not(feature = "ferrocene_certified"))]
393pub mod random;
394#[unstable(feature = "new_range_api", issue = "125687")]
395#[cfg(not(feature = "ferrocene_certified"))]
396pub mod range;
397pub mod result;
398pub mod sync;
399#[unstable(feature = "unsafe_binders", issue = "130516")]
400#[cfg(not(feature = "ferrocene_certified"))]
401pub mod unsafe_binder;
402
403pub mod fmt;
404#[cfg(not(feature = "ferrocene_certified"))]
405pub mod hash;
406#[cfg(not(feature = "ferrocene_certified"))]
407pub mod slice;
408#[cfg(not(feature = "ferrocene_certified"))]
409pub mod str;
410#[cfg(not(feature = "ferrocene_certified"))]
411pub mod time;
412
413#[cfg(not(feature = "ferrocene_certified"))]
414pub mod unicode;
415
416/* Async */
417#[cfg(not(feature = "ferrocene_certified"))]
418pub mod future;
419#[cfg(not(feature = "ferrocene_certified"))]
420pub mod task;
421
422/* Heap memory allocator trait */
423#[allow(missing_docs)]
424pub mod alloc;
425
426// note: does not need to be public
427mod bool;
428#[cfg(not(feature = "ferrocene_certified"))]
429mod escape;
430#[cfg(not(feature = "ferrocene_certified"))]
431mod tuple;
432#[cfg(not(feature = "ferrocene_certified"))]
433mod unit;
434
435#[stable(feature = "core_primitive", since = "1.43.0")]
436#[cfg(not(feature = "ferrocene_certified"))]
437pub mod primitive;
438
439// Pull in the `core_arch` crate directly into core. The contents of
440// `core_arch` are in a different repository: rust-lang/stdarch.
441//
442// `core_arch` depends on core, but the contents of this module are
443// set up in such a way that directly pulling it here works such that the
444// crate uses the this crate as its core.
445#[path = "../../stdarch/crates/core_arch/src/mod.rs"]
446#[allow(
447    missing_docs,
448    missing_debug_implementations,
449    dead_code,
450    unused_imports,
451    unsafe_op_in_unsafe_fn,
452    ambiguous_glob_reexports,
453    deprecated_in_future,
454    unreachable_pub
455)]
456#[allow(rustdoc::bare_urls)]
457#[cfg(not(feature = "ferrocene_certified"))]
458mod core_arch;
459
460#[stable(feature = "simd_arch", since = "1.27.0")]
461#[cfg(not(feature = "ferrocene_certified"))]
462pub mod arch;
463
464// Pull in the `core_simd` crate directly into core. The contents of
465// `core_simd` are in a different repository: rust-lang/portable-simd.
466//
467// `core_simd` depends on core, but the contents of this module are
468// set up in such a way that directly pulling it here works such that the
469// crate uses this crate as its core.
470#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
471#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
472#[allow(rustdoc::bare_urls)]
473#[unstable(feature = "portable_simd", issue = "86656")]
474#[cfg(not(feature = "ferrocene_certified"))]
475mod core_simd;
476
477#[unstable(feature = "portable_simd", issue = "86656")]
478#[cfg(not(feature = "ferrocene_certified"))]
479pub mod simd {
480    #![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
481
482    #[unstable(feature = "portable_simd", issue = "86656")]
483    pub use crate::core_simd::simd::*;
484}
485
486include!("primitive_docs.rs");