-2

If I do

fn split(text: String) -> Vec<str> {

It tells me this

29  | fn split(text: String) -> Vec<str> {
    |                           ^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `str`

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
AMTitan
  • 1
  • 3
  • your function must have a return type: `fn myfn() -> Vec` – Svetlin Zarev Jun 28 '21 at 18:54
  • @SvetlinZarev If I do that it gives me more errors – AMTitan Jun 28 '21 at 18:58
  • I didn't search for a duplicate, but there are plenty. You are trying to return a reference from a function, but a reference to what? Who owns the original `String` which those `&str`s are slices of? Based on the type, it must be the function itself, since it owns the `String` input. You can't do that because the references will become invalid as soon as the function returns. – Peter Hall Jun 28 '21 at 19:00
  • @AMTitan It's really hard to tell what you are trying to achieve without the whole function body. `&str` is a reference to a string slice, but what should this reference refer to ? It's not possible to tell from the currently available code – Svetlin Zarev Jun 28 '21 at 19:01
  • Perhaps this: https://stackoverflow.com/questions/32682876/is-there-any-way-to-return-a-reference-to-a-variable-created-in-a-function/32683309#32683309 – Peter Hall Jun 28 '21 at 19:01
  • I think you should start with the rust book: https://doc.rust-lang.org/book/ – Svetlin Zarev Jun 28 '21 at 19:04
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Jun 28 '21 at 19:13

1 Answers1

0

100% not the best way to do this but I did this

fn split(text: String) -> Vec<String> {
    let mut output_string:Vec<String> = Vec::new();
    
    for x in output {
        output_string.push(String::from(x));
    }
    
    return output_string;
AMTitan
  • 1
  • 3