Abstract: Functional semiconductor heterojunctions are fundamental units for building up advanced optoelectronics and circuits. Halide perovskites, representing a new class of semiconductors with soft and reconfigurable ionic bonding, hold promise for a variety of applications because of their many unusual, tunable physical properties. This paper reports the formation of the current-rectifying p-n heterojunction in single-crystalline CsSnI3 nanowires via localized phase transition between the n-type yellow and p-type black phases. We attribute the distinction of majority carrier types in these two phases to the different formation energies of the cation and anion vacancies. The present approach to heterojunction formation could inspire deeper understanding of phase-transition dynamics and enable precise control over the design of functional heterostructures using halide perovskite building blocks.