The video offers a sharp analysis of the cognitive friction between array length and indexing, illustrating how modern languages like Rust and Zig attempt to bridge this gap. It is a thoughtful exploration of why such a fundamental concept remains a primary source of logic errors in systems programming.
深度探索
先修知识
- 暂无数据。
后续步骤
- 暂无数据。
深度探索
i fixed ranges forever!本站添加:
There are two hard problems in computer science, cache invalidation, naming things, and off-by-one errors. This was a famous joke in the programming communities. And as you probably noticed, the joke said two, but counted three. The joke itself has an off-by-one error, just like arrays. Imagine you have an array with 10 elements representing the numbers from zero to nine. If you run this loop, it'll print zero, then one, and so on all the way to nine, which is the last element in the array. But, it won't stop there. It'll keep going to 10, which would be the 11th element, not the 10th. And that's where the problem happens. And the reason is simple. This length returns 10, not nine. Think about it simply.
Imagine you have two arrays, one empty, and one with just a single element. The empty one naturally has a length of zero as an indication that it has no elements, nothing inside to read, modify, or even attempt to iterate over.
But, the array with one element, if its length also returns zero, then what's the difference between the two? If both return zero, that's a logical error in the first place. So, both of them can't return zero. That's not an index, that's a length. But, on the flip side, when you try to iterate over arrays using ranges instead of just putting the array in the for loop directly like this, when you use ranges, you have to manually specify a start point and an end point.
The start point isn't the issue. The natural starting point for indexing in programming is zero, not one. But, the problem isn't the start point. The real problem is the end point. You don't always know the length of the array, and it's not reasonable to calculate it manually, even if it seems simple when you're writing a simple code. But, as someone who's spent his life writing code, I can tell you that's a terrible and painful idea, and it leads to another well-known problem called magic numbers. The number you write manually will need to be changed manually every time the array length changes, and if you need it somewhere else, you'd have to write it again, and so on. And here someone might say, "Why not just make it a constant and store it?" Well, sure.
Let's go back to using assembly. I will stop building modern programming languages and I will open a cigarette stand since you want to do every obvious step by hand and make everyone's life miserable. Anyway, it can't be a magic number or even a constant. The right approach is for the language itself to provide something like array.length.
But, here's where the real problem lies.
The array.length returns 10 and the last valid index is nine. So, if you use array.length in a range, you'd have to write this, which forces the programmer to write minus one after every length.
In a way that's honestly just as ugly as everything we tried before. This is not a solution. This is just a workaround.
However, before I show you the solutions, I need to make something clear. None of this is actually a technical problem. In theory, you can avoid all of it pretty easily. But, in practice, the world is full of idiots.
Human error never ends and that's exactly why this stuff matters. And I'm definitely not the first person to talk about this. Over the years, people showed up with strange ideas and even stranger languages. But, in this episode, I'm only going to focus on things I've personally lived through, not everything that ever happened in history. Anyway, let's start with Zig.
Zig has the strangest range model I've ever seen in my life. In the for loop, ranges are exclusive. So, if you write a range from zero to 10, that means zero to nine, not 10, which solves the length issue we just talked about. So far, so good. But, if you open a switch statement and try to use the same range you're used to, you will get a weird error right in your face. Exclusive ranges don't work in switch statements.
They're only for the for loops and slices. And that alone is already strange. But, the way they solved it, that's what I find genuinely bizarre.
They added a new type of range, inclusive, includes the last number, and defined it with three dots instead of two. So, now we have two dots in for loop and three dots in switch. Different behavior, different places, but same language. And honestly, even after years of using Zig daily, my brain still doesn't accept this. I can't internalize it. A few years ago, I went looking and found out I wasn't alone. Andrew Kelley, and the creator of Zig himself, opened a GitHub issue about exactly this problem.
In 2017, he saw it, flagged [snorts] it, and proposed fixes. And the community debated it for years. But after 4 years, in 2021, the issue got closed without a real solution. We're now in 2026, and it's still there, being ignored in a way I find genuinely infuriating. But actually, here's where it gets interesting, because Rust had the exact same problem. But unlike Zig, they actually did something about it. For years, exclusive ranges and match patterns didn't exist in Rust, either.
Same situation as Zig. You could only use inclusive ranges in patterns. And the reason was that the Rust team worried exclusive ranges could introduce more confusion and more off-by-one errors in pattern matching. But in July 2024, Rust finally stabilized exclusive range patterns after years of the feature remaining unstable. And the reason they could do it safely? Well, I think they built proper exhaustiveness checking around it. Lints that warn you when your ranges have gaps or overlaps.
And that's exactly my point. Zig's argument is that exclusive ranges in switch statements create ambiguity. So, they simply don't allow them. Rust had the exact same concern. But instead of blocking the feature entirely, they improved the tooling around it. Because the real problem was never the exclusive range itself. The real problem was that the compiler wasn't smart enough yet to catch the mistakes. And that's an implementation problem, not a design problem. The range should be an expression, a first-class value in the language. You should be able to write it, store it, pass it around, and use it anywhere. And if you use it incorrectly, if your patterns have gaps or overlaps, the compiler should tell you, not you can't use this, but you used this incorrectly. That's the difference between a language that protects you and a language that restricts you. And speaking of restrictions, I think I made a mistake when I made while true return a compiler error. But after thinking about it philosophically, and after everything we just talked about with Zig and what they did with ranges, I realized that I used the exact same principle of restriction and stubbornness in a place where it never belonged. So, I'm taking that back.
While true is allowed. No unnecessary restrictions. I know my original argument was about forcing people to use loop for infinite loops instead of using while true as a workaround. And personally, I still prefer that style, but the right call is to leave the decision to the programmer. Anyway, back to ranges. In the loop episode, I used two dots for exclusive and two dots with equals for inclusive, exactly like Rust.
And then the comments exploded. A lot of people pushed back and said that exclusive by default is visually wrong and confusing. And because I saw so many comments, I rushed and changed the syntax in the next episode to make it inclusive by default with two dots and added a less than sign to exclude the last number and greater than sign for reversed ranges. Same idea. Just the direction changes based on the range type. But even after the changes, the comments exploded again. But this time, the arguments were actually solid and they were all connected to the off-by-one error problem, exactly like we just talked about. So, I ran a poll on the channel to see which style people actually preferred, the old one or the new one. But before I show you the results and give you my final answer, I'd really appreciate a few seconds of your time to introduce you to today's sponsor, Send. If you've ever built anything that sends messages to users, OTP, notifications, or anything else, you know the headache. You pick SMS, then someone asks for WhatsApp, then RCS, and suddenly you're managing three different clients, three sets of credentials, three different APIs. Send fixes this with one API call. You pass a phone number and a message. That's it.
Send figures out the best channel to reach that person based on availability, engagement history, and cost. You don't pick the channel, and your code stays exactly the same whether the message goes out on SMS, WhatsApp, or RCS. Send handles carrier routing, formatting, fallbacks, and compliance automatically.
And the pricing is different, too. Most APIs charge per message, so every send is a cost decision. Send charges per contact per month, regardless of how many messages you send them or which channel they go out on. Most teams switching from Twilio save around 40%.
Getting started takes under 5 minutes.
Verify your phone and email, add a card, but you won't be charged. You will get $5 free credit automatically, enough to test the whole thing right now. And if you want your own number and sender identity, Send's team will walk you through the whole setup. I will leave the link as in the description.
Anyway, let's go back to the poll results. 59% voted for the old syntax, and 41% for the new one. But the numbers aren't the interesting part, the comments are. The people who voted for the old syntax had one consistent argument, and it's exactly what we talked about today, the off-by-one problem. One comment put it simply, "Iterating open ranges is more common.
An array of length n has its last element on n - 1." That's it. That's the whole argument, and it's correct. The people who voted for the new syntax had a different argument, that two dots range reading as 0 to 10 is more natural. And honestly, I understand that. It makes sense as a first instinct. But there's a difference between what feels natural when you read a number and what's actually safer when you write code, and that's my answer, too. Not because of the vote, not because of the majority, but because the argument is stronger. The two dots range gives you 0 to 9, exclusive end. The two dots equals range gives you 0 to 10, inclusive end. That's it. Final answer, forever.
I think that's enough for today, but there's something that happened after the last video dropped. The amount of reports that hit the channel caused YouTube to restrict the reach of my videos. So, I made a gift for everyone who reported the channel, sent me hate in the comments, or even in some Discord servers.
I hope you'll accept my apology.
>> May Sura, May Sura, clear the lane.
[music] The future is green. May Sura, May Sura, bow down [music] to the sultan of the screen. I was carving raw code in the dark straight into the stone.
[music and singing] C, P, D's rust and zig, yeah, I ruled that throne. But the system felt heavy, the legacy chain was a crime. [music and singing] So, I flipped the whole script and I coded mine. They got mad at the genius, they couldn't endure the sparks, started [music] throwing the shade, barking loud from the dark, called [singing] my syntax a failure, [music] called my vision a flaw. Look me dead in the eyes and they whispered, "AI slop." Clearly I, they are stuck in the past. You are trapped in the matrix, fading out fast.
[music] I don't follow the rules, I'm the one who creates. Now, watch the compiler [music] slam SHUT ON YOUR FACE. I'm number one, sultan of the screen, yeah, my time has begun.
>> [music] >> From the low level dust to the top of the world, look at what I have done.
You can bark, [music] you can hate, but you can't stop the shine. The empire is spinning and the future is mine, [music] mine, mine. Everything is mine.
>> [music] >> Mine, mine.
>> [music] >> Everything is mine.
Every under listening, every dev in the zone. [singing] If they pushed you aside, go and build your own throne.
>> [music] >> They wanted [music] me broken, they wanted me dead in the queue, but I patched up the bugs and I'm coming for you.
>> [music] >> Your servers are crashing, your legacy's done. I'm the ghost in the system, the ultimate one.
>> [music] >> No control Z can save you from what's coming next. I'm rewriting the [singing] world, one line of text. AI slop now, this is raw grit. Delete the haters, save changes.
>> Well, that was a bit much, but my ADHD really liked that part, but my OCPD thinks the next one is better.
>> Born in [music] the dark where the streetlights failed, no silver spoon, just a digital trail. Lonely code in a room with no air. Myzhar as hands were building a snare. [music] Myzhar as hands were building a snare.
One byte, two bytes, [music] the hunger was real. Poverty's gripped with a fist of steel. Now the terminal glows, the screen is his [music] throne. The sultan of silicon stands all alone.
Myzhar, sultan of [music] the screen, the ghost in the wire, the ultimate machine. From the mud to the cloud, the empire is vast.
Breaking, [music] Myzhar, feel the pulse in the code. The king of the kingdom that nobody knows.
相关推荐
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











