1) convenient passing arguments to println!; 2) difference between slice and &str

Error "mismatched types
expected &str, found slice [&str]rustc(E0308)
main.rs(191, 25): expected &str, found slice [&str]"
for example

let a= vec!["d","dd","jds"];
let aa = a[1..3];
let vv = parse_pair(aa,',');

//below func def
also Error:
"mismatched types
expected str, found slice
note: expected reference &str
found reference &[std::string::String]rustc(E0308)
main.rs(189, 25): expected str, found slice"

(from one vector):init now is Vec

let v = &init[2..3]; //From book_
fn parse_pair<T: FromStr>(s : &str, separator :char) -> Option<(T,T)>{
    match s.find(separator){
        None => None,
        Some(index) => {
            match (T::from_str(&s[..index]), T::from_str(&s[index+1..])){
                (Ok(l),Ok(r)) => Some((l, r)),
                _ => None
            }
    }
}}

2I have vector with smth,want to unpack him convinient but can't imagine how(al names only from structure,not important)

write!(&mut file, "equation_type:{data1}  {sep} \n
        add_arg:{data2}  {sep} \n
        margin_domain:{data3:?} {sep} \n
        time_eval_period_stage:{data4} {sep} \n
        bound_type:{data5}  {sep}  \n
        init_conditions:{data6:?} {sep} \n
        quantity_split_nodes:{data7} {sep} \n
        n_corant:{data8}  \n",data1 = init[0], data2 = init[1] , data3 = init[2],//parse_pair(&init[3],","),
        data4 = init[4], data5 = init[5], data6 =init[6],// parse_three(String::as_str(String::from(init[7])),","),  
        data7 = init[8],
        data8 = init[9],sep = sgn).unwrap();

3

 let mut init: Vec<String> = 
//.......contents.split("\n").map(|x| x.trim()).map(str::to_string).collect();
//where content it is text in file
let new_init: &mut Vec<String> = &mut init;
    for x in new_init{
        println!("{:?}",x.retain(|c| c==','));
       /*let y = x.retain(|c| c !=',').as_str();
        init[0].push_str(y);*/
    }

This print () always,what's wrong?)

Please format your post according to the pinned topic, it's currently very hard to read.

1 Like

Okay,you always ready for help,thanks)

  1. To answer your question as it is: a slice is a sequence of items stored in come continuous memory. Most often, as it is in your case, this memory is backed by Vec. To say something about error - well, it's hard to tell what did you want to do with this code, since for now you're trying to pass a bunch of strings into function expecting a single string.
  2. Again, I'm not sure what are you trying to do. What is init?
  3. String::retain works by modifying the string, not by returning a modified string.
1 Like

1Yes,I haven't regarded it, but i want to make it one and pass
2I had corrected a little,want like unpack it on the every odd position(for example)
3But it is simple vector of strings containing , , and i want remove them, but saw, however some kind ().
BUT: With replace it works,what's wrong?)

Rule of thumb: if you want your program do something - tell it to do something, it won't be done automagically. You want to join multiple strings into one, with commas in between? a[1..3].join(',').

What do you mean by "unpack"? What are the expected inputs and outputs of this operation?

x is a String, not a Vec.

You're using retain, which means "leave only this, drop anything else".

As I said before (and as is said in the linked docs), String::retain modifies the string, it doesn't return anything informative. () is a so-called "unit type", used in any place where there's no information except the fact that the code was run.

2 as in function write, i want output
4thanks))

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.