Issue with printing ‍`char`s

⚓ Rust    📅 2025-12-22    👤 surdeus    👁️ 1      

surdeus

Hello,
I'm learning rust with The Book, in the end of chapter 8.3 of the book, there's some suggested projects to build, I was doing the Latin Pig project, but I came across following error,


error[E0277]: the size for values of type `[char]` cannot be known at compilation time
  --> src/main.rs:21:32
   |
21 |         format!("{:?}-{:?}ay", characters[1..], characters[0])
   |                  ----          ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |                  |
   |                  required by this formatting parameter
   |
   = help: the trait `Sized` is not implemented for `[char]`
   = note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `pig_latin` (bin "pig_latin") due to 1 previous error

Here's my code (happy to hear if my way wasn't correct and my above question was kinda an xy problem because how I managed to make a vector of characters wasn't a good way of implementing the problem.)

fn main() {
    let word1 = "first";
    let word2 = "apple";
    println!("{} {}", pig_latin(word1), pig_latin(word2));
}

fn pig_latin(word: &str) -> String {
    let vowels = ['a', 'e', 'u', 'i', 'o'];
    let mut characters = Vec::new();
    for character in word.chars() {
        characters.push(character);
    }
    let is_first_letter_vowel = if vowels.contains(&characters[0]) {
        true
    } else {
        false
    };
    let result = if is_first_letter_vowel {
        format!("{word}-hay")
    } else {
        format!("{:?}-{:?}ay", characters[1..], characters[0])
    };
    result
}

Thanks!

2 posts - 2 participants

Read full topic

🏷️ Rust_feed