I would like to understand "the size of values of type xyz cannot be known at compile time"

Hello,

I tried to find out about my question via a quick google search but I did not find something closely related, the suggestions in similar questions also did not help. I want to understand the compiler error stated in the topic headline. Thanks!

I am following the Rust book Developing the Library’s Functionality with Test Driven Development - The Rust Programming Language and write unit tests for the mini grep project from chapter 12.
My directory structure is:

src/
- bin/
-- lib.rs
-main.rs
test_setup/
- mod.rs
Cargo.toml
// lib.rs
mod test_setup;

#...

#[cfg(test)]
mod tests {
    use crate::Config;

    #[test]
    fn test_build_config() {
        // use the setup method generate_cmd_args() here.
        assert_eq!(Config::build(&args), Err("Not enough arguments."));
    }
}
//test_setup/mod.rs

// compiler error at [[String]]: 
// the size for values of type `[[String]]` cannot be known at compilation time
// the trait `Sized` is not implemented for `[[String]]`
fn generate_cmd_args() -> [[String]] {
        let file_path = "mypath/Rust/Tutorials/minigrep/searchfile";
        let minigrep_path = "mypath/Rust/Tutorials/minigrep/";
        let search_text = "keyword";
        let args = [[String::from(minigrep_path), String::from(file_path)]];
        args

        
}

The relevant part from the book is this one: Advanced Types - The Rust Programming Language.

Dinamically sized types (DST) are types like str or dyn MyTrait that need some form of indirection (i.e. a & or a Box). It's hard to say anything beyond what's already explained in the book chapter linked above, so feel free to get back if you have any further questions on the subject!

1 Like

Does it make sense to skip all the chapters or does this chapter require me to have understood some?

Given that DSTs are in chapter 20 and you're currently in chapter 12, I would take my time reading the book in the expected order.

There's probably a chapter on traits before chapter 20 that might be useful to read, but can't tell for sure (I read it around 6 years ago :smile:).

1 Like

thanks :slight_smile:

1 Like