Article View: comp.arch
Article #20Re: Optimization by programmers
From: EricP
Date: Thu, 08 Oct 2026 02:13
Date: Thu, 08 Oct 2026 02:13
54 lines
2857 bytes
2857 bytes
On 2026-Oct-07 16:49, EricP wrote: > On 2026-Oct-07 15:25, Michael S wrote: >> https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=array#invalid-array-element-access > > Does Rust allow zero length arrays, vectors, strings and slices? > Ada calls these a null slice. > In DEC Fortran 77 a zero length slice was a bounds check error, > which was really irritating because in string manipulation they happen a lot. > If so, how does one specify a null slice in Rust? > I couldn't find any definition in that doc. > > I asked the Google AI "does programming language rust support zero length array slices" and it said: Yes, Rust fully supports zero-length (empty) array slices. A slice in Rust ([T]) represents a dynamically-sized view into a contiguous sequence of elements. Because the length is evaluated at runtime, a slice can safely have a length of 0. How to Create an Empty Slice You can create or obtain a zero-length slice in Rust in several ways: From an empty array or vector: let empty_array: [i32; 0] = []; let slice: &[i32] = &empty_array; // A zero-length slice Using a range that yields no elements: let numbers = [1, 2, 3, 4]; let empty_slice = &numbers[2..2]; // Length is 0 Directly via standard library constants: let empty_slice: &[i32] = &[]; Memory Layout of a Zero-Length Slice Under the hood, a slice reference (&[T]) is a fat pointer consisting of two components: 1. A pointer to the data. 2. The length of the slice (usize). For a zero-length slice, the length component is 0. To avoid using a null pointer (which Rust references cannot be), the data pointer component points to a well-aligned, non-null "dangling" pointer address (specifically NonNull::dangling()), making it completely safe and overhead-free.
Technical Details
Message-ID:
<A%CxS.2342$kpNb.464@fx16.iad>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mailReferences:
<1177fdi$26ea4$1@dont-email.me> <86bj9hr91x.fsf@linuxsc.com> <119dne7$2baig$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com> <QfyxS.548$cMed.68@fx10.iad>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: EricP <ThatWouldBeTelling@thevillage.com> Subject: Re: Optimization by programmers Newsgroups: comp.arch References: <1177fdi$26ea4$1@dont-email.me> <86bj9hr91x.fsf@linuxsc.com> <119dne7$2baig$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com> <QfyxS.548$cMed.68@fx10.iad> Content-Language: en-CA In-Reply-To: <QfyxS.548$cMed.68@fx10.iad> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 54 Message-ID: <A%CxS.2342$kpNb.464@fx16.iad> X-Complaints-To: abuse@UsenetServer.com NNTP-Posting-Date: Thu, 08 Oct 2026 02:13:52 UTC Date: Wed, 7 Oct 2026 22:13:50 -0400 X-Received-Bytes: 2625