@@ -438,17 +438,30 @@ impl<'a> From<Vec<Spans<'a>>> for Text<'a> {
438438
439439impl<'a> From<Text<'a>> for String {
440440 fn from(text: Text<'a>) -> String {
441− let lines: Vec<String> = text.lines.iter().map(String::from).collect();
442− lines.join("\n")
441+ String::from(&text)
443442 }
444443}
445444
446445impl<'a> From<&Text<'a>> for String {
447446 fn from(text: &Text<'a>) -> String {
448− let lines: Vec<String> = text.lines.iter().map(String::from).collect();
449− lines.join("\n")
447+ let size = text
448+ .lines
449+ .iter()
450+ .flat_map(|spans| spans.0.iter().map(|span| span.content.len()))
451+ .sum::<usize>()
452+ + text.lines.len().saturating_sub(1); // for newline after each line
453+ let mut output = String::with_capacity(size);
454+
455+ for spans in &text.lines {
456+ for span in &spans.0 {
457+ output.push_str(&span.content);
458+ }
459+ output.push('\n');
460+ }
461+ output
450462 }
451463}
464+
452465impl<'a> IntoIterator for Text<'a> {
453466 type Item = Spans<'a>;
454467 type IntoIter = std::vec::IntoIter<Self::Item>;