From 3d0233bc7d9979bfb3f7e4dcea84b04f20ad61ab Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Wed, 11 Sep 2024 21:02:18 +0800 Subject: [PATCH] const expression can borrow static items --- src/items/constant-items.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/items/constant-items.md b/src/items/constant-items.md index 9fb218519..1224fe765 100644 --- a/src/items/constant-items.md +++ b/src/items/constant-items.md @@ -91,6 +91,32 @@ m!(const _: () = ();); // const _: () = (); ``` +## Use and reference to `static` items + +When a constant item or constant block is defined, [`static` items] can be used, borrowed or taken address of. +By extension, you are allowed to call methods that immutably borrows the `static` items as receivers. + +```rust +static A: u32 = 32; +const ANOTHER_A: u32 = A; +const BORROW_A: &'static u32 = &A; +const POINTER_TO_A: *const u32 = &A as _; + +struct MyStruct { + inner: u32, +} +impl MyStruct { + const fn get(&self) -> u32 { + self.inner + 1 + } +} +static MYSTRUCT: MyStruct = MyStruct { + inner: 0 +}; +const BORROW_STATIC_INNER: &'static u32 = &MYSTRUCT.inner; +const CALL_CONST_STATIC_ASSOCIATED_METHOD: u32 = MYSTRUCT.get(); +``` + ## Evaluation [Free][free] constants are always [evaluated][const_eval] at compile-time to surface @@ -111,6 +137,7 @@ fn unused_generic_function() { [constant value]: ../const_eval.md#constant-expressions [free]: ../glossary.md#free-item [static lifetime elision]: ../lifetime-elision.md#static-lifetime-elision +[`static` items]: ./static-items.md [trait definition]: traits.md [IDENTIFIER]: ../identifiers.md [underscore imports]: use-declarations.md#underscore-imports