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// Since core defines many fundamental lang items, all tests live in a
47// separate crate, coretests (library/coretests), to avoid bizarre issues.
48//
49// Here we explicitly #[cfg]-out this whole crate when testing. If we don't do
50// this, both the generated test artifact and the linked libtest (which
51// transitively includes core) will both define the same set of lang items,
52// and this will cause the E0152 "found duplicate lang item" error. See
53// discussion in #50466 for details.
54//
55// This cfg won't affect doc tests.
56#![cfg(not(test))]
57//
58#![stable(feature = "core", since = "1.6.0")]
59#![doc(
60    html_playground_url = "https://play.rust-lang.org/",
61    issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
62    test(no_crate_inject, attr(deny(warnings))),
63    test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
64)]
65#![doc(rust_logo)]
66#![doc(cfg_hide(
67    not(test),
68    no_fp_fmt_parse,
69    target_pointer_width = "16",
70    target_pointer_width = "32",
71    target_pointer_width = "64",
72    target_has_atomic = "8",
73    target_has_atomic = "16",
74    target_has_atomic = "32",
75    target_has_atomic = "64",
76    target_has_atomic = "ptr",
77    target_has_atomic_equal_alignment = "8",
78    target_has_atomic_equal_alignment = "16",
79    target_has_atomic_equal_alignment = "32",
80    target_has_atomic_equal_alignment = "64",
81    target_has_atomic_equal_alignment = "ptr",
82    target_has_atomic_load_store = "8",
83    target_has_atomic_load_store = "16",
84    target_has_atomic_load_store = "32",
85    target_has_atomic_load_store = "64",
86    target_has_atomic_load_store = "ptr",
87))]
88#![no_core]
89#![rustc_coherence_is_core]
90#![rustc_preserve_ub_checks]
91//
92// Lints:
93#![deny(rust_2021_incompatible_or_patterns)]
94#![deny(unsafe_op_in_unsafe_fn)]
95#![deny(fuzzy_provenance_casts)]
96#![warn(deprecated_in_future)]
97#![warn(missing_debug_implementations)]
98#![warn(missing_docs)]
99#![allow(explicit_outlives_requirements)]
100#![allow(incomplete_features)]
101#![warn(multiple_supertrait_upcastable)]
102#![allow(internal_features)]
103#![deny(ffi_unwind_calls)]
104#![warn(unreachable_pub)]
105// Do not check link redundancy on bootstraping phase
106#![allow(rustdoc::redundant_explicit_links)]
107#![warn(rustdoc::unescaped_backticks)]
108//
109// Library features:
110// tidy-alphabetical-start
111#![feature(array_ptr_get)]
112#![feature(asm_experimental_arch)]
113#![feature(bigint_helper_methods)]
114#![feature(bstr)]
115#![feature(bstr_internals)]
116#![feature(closure_track_caller)]
117#![feature(const_carrying_mul_add)]
118#![feature(const_eval_select)]
119#![feature(core_intrinsics)]
120#![feature(coverage_attribute)]
121#![feature(disjoint_bitor)]
122#![feature(internal_impls_macro)]
123#![feature(ip)]
124#![feature(is_ascii_octdigit)]
125#![feature(lazy_get)]
126#![feature(link_cfg)]
127#![feature(non_null_from_ref)]
128#![feature(offset_of_enum)]
129#![feature(panic_internals)]
130#![feature(ptr_alignment_type)]
131#![feature(ptr_metadata)]
132#![feature(set_ptr_value)]
133#![feature(slice_as_array)]
134#![feature(slice_as_chunks)]
135#![feature(slice_ptr_get)]
136#![feature(str_internals)]
137#![feature(str_split_inclusive_remainder)]
138#![feature(str_split_remainder)]
139#![feature(ub_checks)]
140#![feature(unchecked_neg)]
141#![feature(unchecked_shifts)]
142#![feature(utf16_extra)]
143#![feature(variant_count)]
144// tidy-alphabetical-end
145//
146// Language features:
147// tidy-alphabetical-start
148#![feature(abi_unadjusted)]
149#![feature(adt_const_params)]
150#![feature(allow_internal_unsafe)]
151#![feature(allow_internal_unstable)]
152#![feature(auto_traits)]
153#![feature(cfg_sanitize)]
154#![feature(cfg_target_has_atomic)]
155#![feature(cfg_target_has_atomic_equal_alignment)]
156#![feature(cfg_ub_checks)]
157#![feature(const_precise_live_drops)]
158#![feature(const_trait_impl)]
159#![feature(decl_macro)]
160#![feature(deprecated_suggestion)]
161#![feature(doc_cfg)]
162#![feature(doc_cfg_hide)]
163#![feature(doc_notable_trait)]
164#![feature(extern_types)]
165#![feature(f128)]
166#![feature(f16)]
167#![feature(freeze_impls)]
168#![feature(fundamental)]
169#![feature(generic_arg_infer)]
170#![feature(if_let_guard)]
171#![feature(intra_doc_pointers)]
172#![feature(intrinsics)]
173#![feature(lang_items)]
174#![feature(let_chains)]
175#![feature(link_llvm_intrinsics)]
176#![feature(macro_metavar_expr)]
177#![feature(marker_trait_attr)]
178#![feature(min_specialization)]
179#![feature(multiple_supertrait_upcastable)]
180#![feature(must_not_suspend)]
181#![feature(negative_impls)]
182#![feature(never_type)]
183#![feature(no_core)]
184#![feature(no_sanitize)]
185#![feature(optimize_attribute)]
186#![feature(prelude_import)]
187#![feature(repr_simd)]
188#![feature(rustc_allow_const_fn_unstable)]
189#![feature(rustc_attrs)]
190#![feature(rustdoc_internals)]
191#![feature(simd_ffi)]
192#![feature(staged_api)]
193#![feature(stmt_expr_attributes)]
194#![feature(strict_provenance_lints)]
195#![feature(trait_alias)]
196#![feature(transparent_unions)]
197#![feature(try_blocks)]
198#![feature(unboxed_closures)]
199#![feature(unsized_fn_params)]
200#![feature(with_negative_coherence)]
201// tidy-alphabetical-end
202//
203// Target features:
204// tidy-alphabetical-start
205#![feature(arm_target_feature)]
206#![feature(avx512_target_feature)]
207#![feature(hexagon_target_feature)]
208#![feature(loongarch_target_feature)]
209#![feature(mips_target_feature)]
210#![feature(powerpc_target_feature)]
211#![feature(riscv_target_feature)]
212#![feature(rtm_target_feature)]
213#![feature(sha512_sm_x86)]
214#![feature(sse4a_target_feature)]
215#![feature(tbm_target_feature)]
216#![feature(wasm_target_feature)]
217#![feature(x86_amx_intrinsics)]
218// tidy-alphabetical-end
219
220// allow using `core::` in intra-doc links
221#[allow(unused_extern_crates)]
222extern crate self as core;
223
224#[prelude_import]
225#[allow(unused)]
226use prelude::rust_2021::*;
227
228#[cfg(not(test))] // See #65860
229#[macro_use]
230mod macros;
231
232// We don't export this through #[macro_export] for now, to avoid breakage.
233// See https://github.com/rust-lang/rust/issues/82913
234#[cfg(not(test))]
235#[unstable(feature = "assert_matches", issue = "82775")]
236/// Unstable module containing the unstable `assert_matches` macro.
237pub mod assert_matches {
238    #[unstable(feature = "assert_matches", issue = "82775")]
239    pub use crate::macros::{assert_matches, debug_assert_matches};
240}
241
242// We don't export this through #[macro_export] for now, to avoid breakage.
243#[unstable(feature = "autodiff", issue = "124509")]
244/// Unstable module containing the unstable `autodiff` macro.
245pub mod autodiff {
246    #[unstable(feature = "autodiff", issue = "124509")]
247    pub use crate::macros::builtin::autodiff;
248}
249
250#[cfg(not(bootstrap))]
251#[unstable(feature = "contracts", issue = "128044")]
252pub mod contracts;
253
254#[unstable(feature = "cfg_match", issue = "115585")]
255pub use crate::macros::cfg_match;
256
257#[macro_use]
258mod internal_macros;
259
260#[path = "num/shells/int_macros.rs"]
261#[macro_use]
262mod int_macros;
263
264#[rustc_diagnostic_item = "i128_legacy_mod"]
265#[path = "num/shells/i128.rs"]
266pub mod i128;
267#[rustc_diagnostic_item = "i16_legacy_mod"]
268#[path = "num/shells/i16.rs"]
269pub mod i16;
270#[rustc_diagnostic_item = "i32_legacy_mod"]
271#[path = "num/shells/i32.rs"]
272pub mod i32;
273#[rustc_diagnostic_item = "i64_legacy_mod"]
274#[path = "num/shells/i64.rs"]
275pub mod i64;
276#[rustc_diagnostic_item = "i8_legacy_mod"]
277#[path = "num/shells/i8.rs"]
278pub mod i8;
279#[rustc_diagnostic_item = "isize_legacy_mod"]
280#[path = "num/shells/isize.rs"]
281pub mod isize;
282
283#[rustc_diagnostic_item = "u128_legacy_mod"]
284#[path = "num/shells/u128.rs"]
285pub mod u128;
286#[rustc_diagnostic_item = "u16_legacy_mod"]
287#[path = "num/shells/u16.rs"]
288pub mod u16;
289#[rustc_diagnostic_item = "u32_legacy_mod"]
290#[path = "num/shells/u32.rs"]
291pub mod u32;
292#[rustc_diagnostic_item = "u64_legacy_mod"]
293#[path = "num/shells/u64.rs"]
294pub mod u64;
295#[rustc_diagnostic_item = "u8_legacy_mod"]
296#[path = "num/shells/u8.rs"]
297pub mod u8;
298#[rustc_diagnostic_item = "usize_legacy_mod"]
299#[path = "num/shells/usize.rs"]
300pub mod usize;
301
302#[path = "num/f128.rs"]
303pub mod f128;
304#[path = "num/f16.rs"]
305pub mod f16;
306#[path = "num/f32.rs"]
307pub mod f32;
308#[path = "num/f64.rs"]
309pub mod f64;
310
311#[macro_use]
312pub mod num;
313
314/* The core prelude, not as all-encompassing as the std prelude */
315
316pub mod prelude;
317
318/* Core modules for ownership management */
319
320pub mod hint;
321pub mod intrinsics;
322pub mod mem;
323pub mod ptr;
324#[unstable(feature = "ub_checks", issue = "none")]
325pub mod ub_checks;
326
327/* Core language traits */
328
329pub mod borrow;
330pub mod clone;
331pub mod cmp;
332pub mod convert;
333pub mod default;
334pub mod error;
335pub mod marker;
336pub mod ops;
337
338/* Core types and methods on primitives */
339
340pub mod any;
341pub mod array;
342pub mod ascii;
343pub mod asserting;
344#[unstable(feature = "async_iterator", issue = "79024")]
345pub mod async_iter;
346#[unstable(feature = "bstr", issue = "134915")]
347pub mod bstr;
348pub mod cell;
349pub mod char;
350pub mod ffi;
351#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
352pub mod io;
353pub mod iter;
354pub mod net;
355pub mod option;
356pub mod panic;
357pub mod panicking;
358#[unstable(feature = "pattern_type_macro", issue = "123646")]
359pub mod pat;
360pub mod pin;
361#[unstable(feature = "random", issue = "130703")]
362pub mod random;
363#[unstable(feature = "new_range_api", issue = "125687")]
364pub mod range;
365pub mod result;
366pub mod sync;
367#[unstable(feature = "unsafe_binders", issue = "130516")]
368pub mod unsafe_binder;
369
370pub mod fmt;
371pub mod hash;
372pub mod slice;
373pub mod str;
374pub mod time;
375
376pub mod unicode;
377
378/* Async */
379pub mod future;
380pub mod task;
381
382/* Heap memory allocator trait */
383#[allow(missing_docs)]
384pub mod alloc;
385
386// note: does not need to be public
387mod bool;
388mod escape;
389mod tuple;
390mod unit;
391
392#[stable(feature = "core_primitive", since = "1.43.0")]
393pub mod primitive;
394
395// Pull in the `core_arch` crate directly into core. The contents of
396// `core_arch` are in a different repository: rust-lang/stdarch.
397//
398// `core_arch` depends on core, but the contents of this module are
399// set up in such a way that directly pulling it here works such that the
400// crate uses the this crate as its core.
401#[path = "../../stdarch/crates/core_arch/src/mod.rs"]
402#[allow(
403    missing_docs,
404    missing_debug_implementations,
405    dead_code,
406    unused_imports,
407    unsafe_op_in_unsafe_fn,
408    ambiguous_glob_reexports,
409    deprecated_in_future,
410    unreachable_pub
411)]
412#[allow(rustdoc::bare_urls)]
413mod core_arch;
414
415#[stable(feature = "simd_arch", since = "1.27.0")]
416pub mod arch;
417
418// Pull in the `core_simd` crate directly into core. The contents of
419// `core_simd` are in a different repository: rust-lang/portable-simd.
420//
421// `core_simd` depends on core, but the contents of this module are
422// set up in such a way that directly pulling it here works such that the
423// crate uses this crate as its core.
424#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
425#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
426#[allow(rustdoc::bare_urls)]
427#[unstable(feature = "portable_simd", issue = "86656")]
428mod core_simd;
429
430#[unstable(feature = "portable_simd", issue = "86656")]
431pub mod simd {
432    #![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
433
434    #[unstable(feature = "portable_simd", issue = "86656")]
435    pub use crate::core_simd::simd::*;
436}
437
438include!("primitive_docs.rs");