package main

import "fmt"

// section: example
func main() {
	// The copy() function copies min(len(src), len(dst)) elements
	// This means it only copies as many elements as will fit

	// Example 1: Copying to a zero-size slice (copies 0 elements)
	fmt.Println("--- Example 1: Copying to zero-size slice ---")
	source1 := []string{"apple", "banana", "cherry"}
	dest1 := []string{} // zero length

	n1 := copy(dest1, source1)
	fmt.Printf("Source: %v (len=%d)\n", source1, len(source1))
	fmt.Printf("Dest:   %v (len=%d)\n", dest1, len(dest1))
	fmt.Printf("Copied: %d elements\n", n1)

	// Example 2: Copying to same-size slice (copies all elements)
	fmt.Println("\n--- Example 2: Copying to same-size slice ---")
	source2 := []string{"dog", "cat", "bird"}
	dest2 := make([]string, 3)

	n2 := copy(dest2, source2)
	fmt.Printf("Source: %v (len=%d)\n", source2, len(source2))
	fmt.Printf("Dest:   %v (len=%d)\n", dest2, len(dest2))
	fmt.Printf("Copied: %d elements\n", n2)

	// Example 3: Copying to larger slice (copies only source elements)
	fmt.Println("\n--- Example 3: Copying to larger slice ---")
	source3 := []string{"red", "green"}
	dest3 := make([]string, 5) // larger than source

	n3 := copy(dest3, source3)
	fmt.Printf("Source: %v (len=%d)\n", source3, len(source3))
	fmt.Printf("Dest:   %v (len=%d)\n", dest3, len(dest3))
	fmt.Printf("Copied: %d elements\n", n3)
	fmt.Println("Note: Remaining elements in dest are zero values")

	// Example 4: Copying to smaller slice (partial copy)
	fmt.Println("\n--- Example 4: Copying to smaller slice (partial copy) ---")
	source4 := []string{"alpha", "beta", "gamma", "delta", "epsilon"}
	dest4 := make([]string, 2) // smaller than source

	n4 := copy(dest4, source4)
	fmt.Printf("Source: %v (len=%d)\n", source4, len(source4))
	fmt.Printf("Dest:   %v (len=%d)\n", dest4, len(dest4))
	fmt.Printf("Copied: %d elements\n", n4)
	fmt.Println("Note: Only the first 2 elements were copied")

	// Example 5: Common pitfall - using capacity instead of length
	fmt.Println("\n--- Example 5: Capacity vs Length pitfall ---")
	source5 := []int{10, 20, 30, 40, 50}
	dest5 := make([]int, 0, 10) // length is 0, but capacity is 10

	n5 := copy(dest5, source5)
	fmt.Printf("Source: %v (len=%d)\n", source5, len(source5))
	fmt.Printf("Dest:   %v (len=%d, cap=%d)\n", dest5, len(dest5), cap(dest5))
	fmt.Printf("Copied: %d elements\n", n5)
	fmt.Println("Note: Even though capacity is 10, length is 0, so nothing is copied!")

	// Solution: ensure destination has correct length
	fmt.Println("\n--- Example 6: Correct approach with length ---")
	source6 := []int{10, 20, 30, 40, 50}
	dest6 := make([]int, len(source6)) // length matches source

	n6 := copy(dest6, source6)
	fmt.Printf("Source: %v (len=%d)\n", source6, len(source6))
	fmt.Printf("Dest:   %v (len=%d, cap=%d)\n", dest6, len(dest6), cap(dest6))
	fmt.Printf("Copied: %d elements\n", n6)
}

// section: example
