0

I have tried multiple things but it is a game of guessing at this point

fn main() {
    let _array: [&'static str; 4] = ["First", "Second", "Third", "Fourth"];

    check_string(/*_array*/);
}

fn check_string(/*_input: ??? */) {


}

1 Answer 1

1

A nice way to check the type of a variable in rust is doing this:

let val: () = /* your value */;

(Playground)
Which gets you an error saying that it expected a () and not a WhatEverYourTypeIs
In this case you would get a [&'static str; 4] as you mention already in your code; so just require that in the function:

fn check_string(data: [&'static str; 4])

You could also pass a slice:

fn check_strings(data: &[&'static str])

And play with the lifetimes:

fn check_strings<'a>(data: &'a[&'a str])

etc.
To call it pass either:

check_string(_array);

If it takes a sized array or

check_strings(&_array[..]);

If it takes the slice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.