Skip to content

Instantly share code, notes, and snippets.

@tai
Created February 3, 2020 12:16
Show Gist options
  • Select an option

  • Save tai/9ddf77ead7d8a191e6d4d3ecd98265cf to your computer and use it in GitHub Desktop.

Select an option

Save tai/9ddf77ead7d8a191e6d4d3ecd98265cf to your computer and use it in GitHub Desktop.
#!/usr/bin/env run-cargo-script
//
// Repeat same character
//
fn main() {
println!("0: {}", (0..40).map(|_| "-").collect::<Vec<_>>().join(""));
println!("1: {}", (0..40).map(|_| "-").collect::<String>());
println!("2: {}", std::iter::repeat("-").take(40).collect::<String>());
println!("3: {}", format!("{:-^1$}", "", 40));
println!("4: {}", format!("{:-^40}", ""));
println!("5: {:-^40}", "");
println!("6: {}", vec!['-'; 40].iter().collect::<String>());
println!("7: {}", vec!["-"; 40].concat());
println!("8: {}", std::str::from_utf8(&[b'-'; 40]).unwrap());
println!("9: {}", unsafe {
&*(&[b'-'; 40] as *const [u8] as *const str) as &str
});
println!("A: {}", "-".repeat(40));
// Does Rust keep length information across casts?
let sv = unsafe {
&*(&[b'-'; 40] as *const [u8] as *const str) as &str
};
println!("len={}", sv.len());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment