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#[unstable(feature = "contracts", issue = "128044")]
251pub mod contracts;
252
253#[unstable(feature = "cfg_match", issue = "115585")]
254pub use crate::macros::cfg_match;
255
256#[macro_use]
257mod internal_macros;
258
259#[path = "num/shells/int_macros.rs"]
260#[macro_use]
261mod int_macros;
262
263#[rustc_diagnostic_item = "i128_legacy_mod"]
264#[path = "num/shells/i128.rs"]
265pub mod i128;
266#[rustc_diagnostic_item = "i16_legacy_mod"]
267#[path = "num/shells/i16.rs"]
268pub mod i16;
269#[rustc_diagnostic_item = "i32_legacy_mod"]
270#[path = "num/shells/i32.rs"]
271pub mod i32;
272#[rustc_diagnostic_item = "i64_legacy_mod"]
273#[path = "num/shells/i64.rs"]
274pub mod i64;
275#[rustc_diagnostic_item = "i8_legacy_mod"]
276#[path = "num/shells/i8.rs"]
277pub mod i8;
278#[rustc_diagnostic_item = "isize_legacy_mod"]
279#[path = "num/shells/isize.rs"]
280pub mod isize;
281
282#[rustc_diagnostic_item = "u128_legacy_mod"]
283#[path = "num/shells/u128.rs"]
284pub mod u128;
285#[rustc_diagnostic_item = "u16_legacy_mod"]
286#[path = "num/shells/u16.rs"]
287pub mod u16;
288#[rustc_diagnostic_item = "u32_legacy_mod"]
289#[path = "num/shells/u32.rs"]
290pub mod u32;
291#[rustc_diagnostic_item = "u64_legacy_mod"]
292#[path = "num/shells/u64.rs"]
293pub mod u64;
294#[rustc_diagnostic_item = "u8_legacy_mod"]
295#[path = "num/shells/u8.rs"]
296pub mod u8;
297#[rustc_diagnostic_item = "usize_legacy_mod"]
298#[path = "num/shells/usize.rs"]
299pub mod usize;
300
301#[path = "num/f128.rs"]
302pub mod f128;
303#[path = "num/f16.rs"]
304pub mod f16;
305#[path = "num/f32.rs"]
306pub mod f32;
307#[path = "num/f64.rs"]
308pub mod f64;
309
310#[macro_use]
311pub mod num;
312
313/* The core prelude, not as all-encompassing as the std prelude */
314
315pub mod prelude;
316
317/* Core modules for ownership management */
318
319pub mod hint;
320pub mod intrinsics;
321pub mod mem;
322pub mod ptr;
323#[unstable(feature = "ub_checks", issue = "none")]
324pub mod ub_checks;
325
326/* Core language traits */
327
328pub mod borrow;
329pub mod clone;
330pub mod cmp;
331pub mod convert;
332pub mod default;
333pub mod error;
334pub mod marker;
335pub mod ops;
336
337/* Core types and methods on primitives */
338
339pub mod any;
340pub mod array;
341pub mod ascii;
342pub mod asserting;
343#[unstable(feature = "async_iterator", issue = "79024")]
344pub mod async_iter;
345#[unstable(feature = "bstr", issue = "134915")]
346pub mod bstr;
347pub mod cell;
348pub mod char;
349pub mod ffi;
350#[unstable(feature = "core_io_borrowed_buf", issue = "117693")]
351pub mod io;
352pub mod iter;
353pub mod net;
354pub mod option;
355pub mod panic;
356pub mod panicking;
357#[unstable(feature = "pattern_type_macro", issue = "123646")]
358pub mod pat;
359pub mod pin;
360#[unstable(feature = "random", issue = "130703")]
361pub mod random;
362#[unstable(feature = "new_range_api", issue = "125687")]
363pub mod range;
364pub mod result;
365pub mod sync;
366#[unstable(feature = "unsafe_binders", issue = "130516")]
367pub mod unsafe_binder;
368
369pub mod fmt;
370pub mod hash;
371pub mod slice;
372pub mod str;
373pub mod time;
374
375pub mod unicode;
376
377/* Async */
378pub mod future;
379pub mod task;
380
381/* Heap memory allocator trait */
382#[allow(missing_docs)]
383pub mod alloc;
384
385// note: does not need to be public
386mod bool;
387mod escape;
388mod tuple;
389mod unit;
390
391#[stable(feature = "core_primitive", since = "1.43.0")]
392pub mod primitive;
393
394// Pull in the `core_arch` crate directly into core. The contents of
395// `core_arch` are in a different repository: rust-lang/stdarch.
396//
397// `core_arch` depends on core, but the contents of this module are
398// set up in such a way that directly pulling it here works such that the
399// crate uses the this crate as its core.
400#[path = "../../stdarch/crates/core_arch/src/mod.rs"]
401#[allow(
402    missing_docs,
403    missing_debug_implementations,
404    dead_code,
405    unused_imports,
406    unsafe_op_in_unsafe_fn,
407    ambiguous_glob_reexports,
408    deprecated_in_future,
409    unreachable_pub
410)]
411#[allow(rustdoc::bare_urls)]
412mod core_arch;
413
414#[stable(feature = "simd_arch", since = "1.27.0")]
415pub mod arch;
416
417// Pull in the `core_simd` crate directly into core. The contents of
418// `core_simd` are in a different repository: rust-lang/portable-simd.
419//
420// `core_simd` depends on core, but the contents of this module are
421// set up in such a way that directly pulling it here works such that the
422// crate uses this crate as its core.
423#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
424#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn)]
425#[allow(rustdoc::bare_urls)]
426#[unstable(feature = "portable_simd", issue = "86656")]
427mod core_simd;
428
429#[unstable(feature = "portable_simd", issue = "86656")]
430pub mod simd {
431    #![doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
432
433    #[unstable(feature = "portable_simd", issue = "86656")]
434    pub use crate::core_simd::simd::*;
435}
436
437include!("primitive_docs.rs");