All guides
Every guide, newest first. Browse by topic instead, or read the daily tips and puzzles.
-
Fix Rust E0308 "mismatched types" — Convert the Value, Fix the Signature, or Give Both Sides One Type Rust
Rust error E0308, mismatched types, means an expression produced one type where a different type was required: an i32 passed where an i64 belongs, if and else arms that disagree, a String where a &str belongs or the reverse, a value where a reference belongs, a stray semicolon turning a return value into (). This guide reproduces the error in its argument, return, branch, string, reference, and unit forms, shows the narrow set of conversions Rust performs automatically and the much larger set it refuses, and gives the fixes in the order to reach for them: change the type at the source, convert with From or Into where the conversion cannot lose data, try with TryFrom where it can, cast with as only with the loss priced in, and borrow or dereference where the mismatch is about access rather than value.
-
Fix Rust E0502 "cannot borrow as mutable because it is also borrowed as immutable" — End the First Borrow, Split the Work, or Restructure Rust
Rust error E0502, cannot borrow as mutable because it is also borrowed as immutable, means a shared borrow is still alive at the point where the code asks for a mutable borrow. This guide reproduces the error in its canonical form, its loop form, and its method-call form, shows the near-identical program the current compiler accepts because the first borrow has already ended, and gives the fixes in the order to reach for them: end or narrow the first borrow, split the work with an index loop or split_at_mut, restructure so a value is returned instead of a reference held, and clone only as a deliberate last resort.
-
Fix Rust E0382 "borrow of moved value" — Borrow Instead of Moving, Return the Value, or Clone on Purpose Rust
Rust error E0382, borrow of moved value, means a value was moved — into a function, another binding, a loop, or a closure — and the code then tried to use the original binding, which no longer owns anything. This guide reproduces the error in its function-call, assignment, loop, closure, and partial-move forms, shows exactly which uses survive a partial move and which do not, and gives the fixes in the order to reach for them: borrow instead of moving, return the value or restructure, take or replace a field, derive Copy only where copying is semantically right, and clone or share ownership only as a deliberate choice.
-
Fix C++ "request for member in pointer type" — Use -> on Pointers, and Why -> Cannot Fix a Non-Class Type C++
The GCC error request for member in a pointer type means the dot operator was used on a pointer: dot requires an object of class type, arrow requires a pointer to class type and is exactly equivalent to dereferencing first. This guide reproduces the dot-on-pointer failure and GCC's maybe you meant to use '->' suggestion, fixes it with the arrow and with the parenthesized dereference form, shows why the unparenthesized star form fails on precedence, covers the sibling non-class type error where the pointee has no members and arrow does not help, and ends with references and std::unique_ptr, where dot and arrow both compile but reach different objects.
-
Fix C++ "no match for 'operator<<'" — Streaming a User-Defined Type and a std::vector of It to std::cout C++
Streaming a user-defined type to std::cout fails with no match for 'operator<<' because std::basic_ostream provides overloads only for built-in types, character data, and a few library types. This guide reads the exact GCC error, fixes it with a non-member operator<< taking (std::ostream&, const T&) and returning std::ostream& so chaining works, shows the friend-defined-inside-class form for private members, adds a separate overload for a std::vector of the type, and shows why a non-const reference parameter breaks streaming of const objects.
-
Fix C++ "invalid conversion from 'const char*' to 'char*'" — Add const, Copy the Text, or Own It in std::string C++
The GCC error invalid conversion from 'const char*' to 'char*' means read-only text was handed to code that claims the right to modify it: a string literal, a const char* variable, or the result of c_str() was assigned to a char* or passed to a char* parameter. This guide reproduces the error in all three forms, explains why GCC reports the literal form with different wording and only as a warning by default, and gives the four correct fixes in order: declare const char* when nothing writes, copy into a local char array when a callee really writes, store the text in std::string and use data() when it must be owned and mutable, and reserve const_cast for the one narrow legacy case where the underlying object is genuinely mutable.
-
Fix C++ "'cout' was not declared in this scope" — Missing
, Missing std::, and the using namespace std Trade-Off C++The error 'cout' was not declared in this scope has two different causes with the same first line: no
included at all, or included but cout used without std:: qualification, because std::cout lives in namespace std and its name is not injected into the global scope. This guide reproduces both failures with GCC, reads the did-you-mean suggestion that only the second failure earns, fixes both with std::cout, covers std::endl versus a newline character, shows the using namespace std form and why it stays out of headers, and ends with the wrong-header trap where provides std::printf but not std::cout. -
Fix C++ "cannot convert 'std::string' to 'const char*'" — c_str(), data(), and When the API Wants char* C++
Passing a std::string where a const char* is expected fails because there is no implicit conversion from std::string to a character pointer. This guide reads the exact GCC error at a real std::fopen call, fixes it with c_str() for read-only APIs, shows why c_str() still fails for a char* parameter while data() on a non-const string works since C++17, and states the invalidation rule that ends the useful life of every pointer these functions return.
-
Fix Go "panic: runtime error: slice bounds out of range" — A High Bound Past Capacity, a Low Bound Past the High Bound, a Full Slice Expression Past Capacity, a String Past Its Length, and a Negative Bound Go
Go panics with slice bounds out of range when a slice expression's bounds break the rule for its form. For a simple slice expression on a slice, the bounds must satisfy 0 <= low <= high <= cap; on an array or a string the high bound is judged against the length instead. A full slice expression must satisfy 0 <= low <= high <= max <= cap. The panic text names the bounds that failed and the capacity or length they were judged against: [:5] with capacity 3, [3:1] with no limit named, [::5] with capacity 3, [:8] with length 5 for a string, and [-1:] for a negative low bound. A constant negative bound never reaches run time, because the compiler rejects it. This guide reproduces each shape exactly as Go 1.26.4 prints it and gives the bounds check that keeps each shape from reaching the panic.
-
Fix Go "panic: sync: negative WaitGroup counter" — Extra Done Calls, Add(-1) Past Zero, and the Add-Before-Go Rule Go
A sync.WaitGroup counter that goes negative panics with panic: sync: negative WaitGroup counter, and every route to it is a counting mismatch: one Done too many, an Add with a negative delta that overshoots zero, a Done on a WaitGroup that was never Added to, or a shared completion callback that fires twice. Done is exactly Add(-1), so the panic names Add in its stack trace even when your code only called Done. The ordering rules prevent the rest: a positive Add when the counter is zero must happen before Wait, Add calls belong before the go statement that creates the work, a reused WaitGroup takes new Add calls only after every previous Wait has returned, and WaitGroup.Go, available since Go 1.25, does the Add and the Done for you so the pair cannot drift apart. Each shape here is reproduced exactly as Go 1.26.4 prints it.
-
Fix Go "panic: runtime error: makeslice: len out of range" — Negative Length, Length Past Capacity, and Sizes That Overflow Go
A slice length that is negative, larger than its capacity, or so large that the runtime cannot allocate it makes make panic at the call itself: panic: runtime error: makeslice: len out of range when the length is the problem, and panic: runtime error: makeslice: cap out of range when the capacity is. The rule underneath both messages is one line: at every moment 0 <= len(s) <= cap(s), and a make that would break it never returns a slice at all. Constants that break the rule never even build — a constant negative length and constant length and capacity in the wrong order are compile errors — while the same values arriving in variables build fine and panic at run time. That split is why this panic survives testing: the numbers that trigger it usually come from input, from a count read off a wire, or from arithmetic that overflowed on the way to the make call. The fixes are all upstream of make: validate any length or capacity that came from outside the program, check size arithmetic for overflow before it reaches make, grow with append instead of pre-sizing from an untrusted number, and cap any preallocation hint at a sane bound. Each shape here is reproduced exactly as Go 1.26.4 prints it, with the check that prevents it.
-
Fix Go "panic: interface conversion" — A String Asserted to Int, a Nil Interface, a Type That Does Not Implement the Target Interface, and a Typed Nil Go
A single-value type assertion that fails panics in Go. Write x.(T) for x of interface type and a type T, and Go asserts the type of the value stored in x is identical to T, or, when T is an interface, that the stored type implements T. Hold, and you get the value stored in x. Fail, and you get a run-time panic instead. The message tells you what was stored and what you asked for: interface {} is string, not int when the concrete type is wrong, interface {} is nil, not int when the interface holds nothing, int is not fmt.Stringer: missing method String when the stored type lacks the method, and interface {} is *int, not int for a typed nil pointer. The comma-ok form of the same assertion does not panic; it returns the zero value for T and false. A type switch handles several possible stored types in one statement. Each shape here is reproduced exactly as Go 1.26.4 prints it, with the form that turns it into a value you can handle.
-
Fix Go "panic: runtime error: comparing uncomparable type []int" — Slice, Map and Function Values as Map Keys and in == Comparisons Go
Slice, map and function types are not comparable in Go: they can only be compared against nil. Written directly, comparing two slices or using a slice as a map key type is a compile error, so the panic in the title only arrives when the value reaches the comparison hidden inside an interface — two any values holding slices, a struct holding a slice compared through an interface, or an array or struct with interface fields. The map-key version of the same mistake prints a different panic, hash of unhashable type, when a slice, map or function value is inserted into or looked up in a map whose key type is an interface. Arrays and structs are comparable exactly when their elements and fields are, which is why the struct shape panics under its own name. The fixes replace the comparison or the key, never the panic: slices.Equal and maps.Equal for contents, reflect.DeepEqual with its nil-versus-empty distinction kept in view, and a comparable key — an array instead of a slice, a string form, an ID, or a struct of comparable fields — where a map key is what the code actually needed. Each shape here is reproduced exactly as Go 1.26.4 prints it, split into the shapes the compiler stops and the shapes that survive to run time.
-
Fix Go "panic: close of nil channel" — the Unmade Channel, and How Nil Channels Block, Disable Select Cases, and Panic on Close Go
panic: close of nil channel means the channel variable was never made: a var declaration, a struct field nobody assigned, or a constructor that returned the declared variable instead of make(chan T). The same nil value explains the rest of the family: a send on a nil channel blocks forever, a receive from a nil channel blocks forever, and a select case on a nil channel is never ready, which is a bug when it is accidental and the standard off switch when it is deliberate. Closing is different: close of a nil channel panics instead of blocking, and close of a made channel is how the sender signals that no more values will be sent. Each shape here is reproduced exactly as Go 1.26.4 prints it, including the contrast that keeps the three states separate: receive from a closed channel returns at once with the zero value and ok set to false, while receive from a nil channel never returns at all.
-
Fix Go "panic: close of closed channel" — A Second Close, a Nil Channel, and the Receives That Stay Safe After Close Go
Closing a channel that is already closed panics in Go. Call close(ch) and Go records that no more values will be sent on ch. Call it a second time and the program dies with panic: close of closed channel. Call it on a nil channel and the message changes by one word: panic: close of nil channel. Neither panic comes from receiving. A receive from a closed channel never panics at all: it drains any values still buffered, then returns the zero value with ok set to false, and a range loop over the channel ends on its own. The fixes follow from who owns the close. Only the sender closes, and it closes exactly once. Where several goroutines can trigger the close, a sync.Once guard makes the first call the only one that runs. Each shape here is reproduced exactly as Go 1.26.4 prints it, with the pattern that prevents it.
-
Fix 'No Matching Function for Call' in C++ — Reading GCC's Candidate List, Viable vs Not Viable, and Conversion Failures C++
The error means overload resolution found candidates for the name but none of them was viable for the arguments given. This guide reads three real GCC failures candidate by candidate — a std::string offered to print(int) and print(const char*), a two-argument call against one-parameter overloads, and a template whose two parameters deduce conflicting types for T — then fixes each one at the coordinate its candidate note names: add the missing overload, call .c_str() for the pointer overload, match the argument count, or state the template argument explicitly.
-
Fix 'Invalid Use of Incomplete Type' in C++ — Forward Declarations, sizeof, and the Pimpl Trap C++
The error means the compiler met a use that needs a type's size or members at a point where only a forward declaration has been seen. This guide reads three real GCC failures clause by clause: member access and sizeof on a forward-declared struct, a std::unique_ptr pimpl whose destructor is defaulted where the implementation type is still incomplete, and a forward-declared base class. It also shows the nearby case that is not this error at all, deleting an incomplete type, which compiles with a warning and undefined behavior instead.
-
Fix 'Passing const as this Argument Discards Qualifiers' in C++ — Const Objects, Const References, and Const Methods C++
The error means a non-const member function was called on an object the compiler must treat as const. This guide reads three real GCC failures — a non-const method on a const object, the same call through a const reference parameter, and a const method that tries to call a non-const helper — then fixes each one at the qualifier that was discarded: make the method const, use mutable only for genuinely internal state, and take a non-const reference only when mutation is the point.
-
Fix a Dangling std::string_view — Lifetime Rules for Views and References to Temporaries C++
A std::string_view taken from a temporary std::string keeps the address and the length after the temporary is destroyed at the end of the statement: the size still reads 40, the text is corrupted or silently becomes another string's text, and AddressSanitizer reports heap-use-after-free with the fault at the print and the free at the assignment. A reference returned from a temporary fails the other way, with a compiler warning and a segfault. This guide reads the report line by line, states the lifetime rule both programs broke, and fixes the code by owning the string at the layer that outlives the view.
-
Read a C++20 Concepts Error — "Constraints Not Satisfied", Clause by Clause C++
A GCC concepts failure looks long because it prints the whole chain from the call site to the one atomic constraint that evaluated to false. This guide reads three real diagnostics clause by clause: a std::integral check failed by a double, a std::signed_integral check where an unsigned int passes the first clause and fails only the second, and a custom concept whose requires-expression names the invalid operation. Clang's shorter wording for the same three failures is shown alongside, and the fixed program passes the same types the diagnostics asked for.
-
Fix 'Cannot Bind Non-const Lvalue Reference to an Rvalue' in C++ — Temporaries, Function Arguments, and const& C++
The error means a non-const lvalue reference was offered a temporary: a literal, an arithmetic result, or any other rvalue that has no lasting name to mutate. This guide reads two real GCC failures clause by clause — binding int& to 42 directly, and passing temporaries to a function taking int& — then shows the two bindings that do accept a temporary, const int& with lifetime extension and int&&, and fixes each call by matching the parameter to the intent: const& for reading, a named lvalue for mutation, and an rvalue reference with std::move for taking ownership.
-
Fix 'Call of Overloaded Function Is Ambiguous' in C++ — Equally Good Conversions, and How Overload Ranking Breaks the Tie C++
The error means overload resolution found two or more viable functions and none of them was better than all the others. This guide reads three real GCC failures rank by rank — an int offered to print(long) and print(double), a two-argument call where each overload wins one argument and loses the other, and a class whose two conversion functions lead to different overloads — then fixes each one at the coordinate that was tied: add the exact-match overload, change one argument so one candidate wins outright, or state the intended conversion explicitly.
-
Fix UBSan "Signed Integer Overflow" Runtime Errors — and Why the Optimizer Cares Sysadmin
UBSan stops a program with 'runtime error: signed integer overflow' when a signed addition, subtraction, or multiplication produces a value that does not fit in its type. This guide reproduces the message exactly as GCC 13.3.0 prints it, explains why signed overflow is undefined behavior rather than a wrap, shows a loop whose overflow guard the optimizer removes at -O2, and gives the fix that belongs to each case: a wider type, unsigned arithmetic where wrapping is intended, or __builtin_add_overflow where the result has to stay an int.
-
Fix ThreadSanitizer "Data Race" Warnings — Read the Two Stacks, Fix the Shared Variable Sysadmin
ThreadSanitizer prints 'WARNING: ThreadSanitizer: data race' when two threads touch the same variable at the same time and at least one access writes, with no synchronization between them. This guide reproduces the smallest write-write race exactly as GCC 13.3.0 prints it, reads the current and previous access stacks, the location line, and the SUMMARY line, and fixes the race twice: once with a mutex around both accesses, and once by making the shared variable atomic, with the measured exit status for each run.
-
Fix Go "panic: send on closed channel" — Close of Closed and Nil Channels, Receiving From a Closed Channel, and select Go
Go panics with send on closed channel when a program sends on a channel after close has been called on it: a sender that outlives the close, a loop that closes inside its own body and sends on the next pass, and a select whose send case names a closed channel all meet the same rule. Closing a channel twice panics with close of closed channel, and closing a nil channel panics with close of nil channel. Receiving runs the other way: a receive from a closed channel returns the values sent before the close and then the zero value at once, with the two-value form reporting ok as false. This guide reproduces each shape exactly as Go 1.26.4 prints it and gives the single-closer ownership, the done channel, and the ok check that keep each shape from reaching the panic.
-
Fix Go "panic: runtime error: invalid memory address or nil pointer dereference" — Nil Receivers, Returned Nil Pointers, and Typed Nil Interfaces Go
Go panics with runtime error: invalid memory address or nil pointer dereference when a program uses a pointer whose value is nil: reading or writing through it, taking a field of the value it would point to, or calling a method that uses its receiver. A declared but never assigned pointer holds nil, a function that returns nil on one path hands the panic to its caller, a pointer field left at its zero value breaks a chain of field accesses one link in, and a typed nil pointer stored in an interface passes a plain nil check and panics later. This guide reproduces each shape exactly as Go 1.26.4 prints it and gives the allocation, the nil check, and the nil-safe method that keep each shape from reaching the dereference.
-
Fix Go "panic: runtime error: integer divide by zero" — Variable Divisors, the Constant-Divisor Compile Error, Modulo, and the Empty Average Go
Go panics with integer divide by zero when an integer division or remainder runs with a divisor that is zero at run time: a variable divisor that arrives as zero, a length used as a divisor on an empty slice, and an unsigned divisor all print the same panic text. A divisor that is a constant zero never reaches run time, because the compiler rejects it with invalid operation: division by zero. Floating-point division by zero does not print this panic in Go 1.26.4: it prints +Inf, -Inf, or NaN. This guide reproduces each shape exactly as Go 1.26.4 prints it and gives the divisor guard and the empty-input check that keep each shape from reaching the panic.
-
Fix Go "panic: runtime error: index out of range" — Slice, Array, String, and Slice-Bounds Panics Go
Go panics with runtime error: index out of range when a program indexes an array, slice, or string outside 0 <= x < len(a), and with slice bounds out of range when a slice expression's low, high, or max bound leaves its allowed range, because the valid index set is set by length for indexing and by capacity for slicing, a constant index that is out of range for an array or a constant string is a compile error instead of a panic, and an index equal to the length is already out. This guide reproduces the panic family exactly as Go 1.26.4 prints it — too-large, negative, and equal-to-length indices, string indexing, simple and three-index slice bounds, and the off-by-one loop — and gives the bounds check, the range loop, and the len/cap reasoning that keep each shape in range.
-
Fix Go "assignment to entry in nil map" Panic — Declared Maps, Struct Fields, and Nested Maps Go
Go compiles a program that assigns to a nil map without a word of complaint and then panics at run time with assignment to entry in nil map, because a map declared with var, a map field inside a zero-value struct, a map a helper returned as nil, and the inner map of a nested map all start life as nil, and a nil map accepts every read — lookup, len, range, delete — while refusing every write. This guide reproduces that panic exactly as Go 1.26.4 prints it and works through the shapes that produce it: the declared-but-never-made map, the struct field nobody initialized, the map made only on one branch, the nil a function hands back to a caller that writes to it, and the inner map of a made outer map — ending with the contrasts that separate this panic from its look-alikes: reading a nil map is safe and returns zero values, and appending to a nil slice works, so the defect is specific to writing into a map that was never made.
-
Fix Go "fatal error: all goroutines are asleep - deadlock!" — Unbuffered Channels, Nil Channels, select, and Unclosed Ranges Go
Go compiles a program whose only goroutine blocks forever on a channel operation and then the runtime kills it with fatal error: all goroutines are asleep - deadlock!, because a receive on an unbuffered channel with no sender, a send with no receiver, an empty select, a receive from a nil channel, a range over a channel nobody closes, and a second receive after a worker sent exactly once all leave every goroutine blocked with nothing left that can wake any of them. This guide reproduces that fatal error exactly as Go 1.26.4 prints it and works through the shapes that produce it — ending with the contrasts that separate a deadlock from a merely blocked program: a goroutine waiting on a timer can still wake, so the detector stays silent, and the fatal error is not a panic, so recover cannot catch it.