O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise - various throughout the chapter

  1. Create a few string slices and Strings, and print them. Use both push and push_str to populate a String with data.

Solution:

  let s1 = "string slice";   let s2 = "another string slice";   let str1 = "heap string".to_string();   let str2 = String::from("another heap string");   let mut str3 = String::new();    str3.push_str("yet");   str3.push(' ');   str3.push_str("another");   str3.push(' ');   str3.push_str("heap string");    println!("s1 {} s2 {} str1 {} str2 {} str3 {}", s1, s2, str1, str2, str3); 
  1. Write a function that takes a string slice and prints it. Pass it a few static string slices, and a few Strings.

Solution:

fn print_string_slice(s: &str) {   println!("Printing {}", s); }  fn main() {  let ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required