krahets/hello-algo · error
очередь пуста
Error message
очередь пуста
What it means
This panic is raised by peek() in an array-backed circular queue (Russian localization). It fires when reading nums[front] while isEmpty() is true. pop() calls peek() internally, so pop-on-empty surfaces here too. The ring buffer has no valid element to return when queSize == 0. @panic is uncatchable.
Source
Thrown at ru/codes/zig/chapter_stack_and_queue/array_queue.zig:77
// С помощью операции взятия по модулю вернуть rear к началу после выхода за конец массива
var rear = (self.front + self.queSize) % self.capacity();
// Добавить num после хвостового узла
self.nums[rear] = num;
self.queSize += 1;
}
// Извлечь из очереди
pub fn pop(self: *Self) T {
var num = self.peek();
// Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива
self.front = (self.front + 1) % self.capacity();
self.queSize -= 1;
return num;
}
// Доступ к элементу в начале очереди
pub fn peek(self: *Self) T {
if (self.isEmpty()) @panic("очередь пуста");
return self.nums[self.front];
}
// Вернуть массив
pub fn toArray(self: *Self) ![]T {
// Преобразовывать только элементы списка в пределах фактической длины
var res = try self.mem_allocator.alloc(T, self.size());
@memset(res, @as(T, 0));
var i: usize = 0;
var j: usize = self.front;
while (i < self.size()) : ({ i += 1; j += 1; }) {
res[i] = self.nums[j % self.capacity()];
}
return res;
}
};
}
View on GitHub (pinned to 69932aed18)
Solutions
- Check queue.isEmpty() before peek() or pop().
- Drive consumption with while (queue.size() > 0).
- Only pop when size() confirms an element exists.
- Wrap pop: if (!queue.isEmpty()) _ = queue.pop();
Example fix
// before var v = queue.pop(); // peeks internally → @panics when empty // after if (queue.isEmpty()) return; var v = queue.pop();
Defensive patterns
Strategy: validation
Validate before calling
// Validate non-empty before peek/pop
if (!queue.isEmpty()) {
var v = queue.peek();
_ = queue.pop();
} Prevention
- pop() delegates to peek() — guard both.
- Bound consumer loops by queue.size(), not capacity.
- Treat @panic as a hard abort with no recovery.
When it happens
Trigger: Calling queue.peek() or queue.pop() on an empty queue (capacity allocated, queSize 0), or after draining all items.
Common situations: Consumer polling faster than producer in a ring buffer; loop bounded by capacity instead of size(); forgetting pop() delegates to peek().
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/7fe58311f5f88190.
Report an issue: GitHub.