Something wrong in function

I wanted to continue modify func in book about rust,original func:

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
            }
        }
    }
}

My(I want to parse insted 2, three arguments) but where //1ая ветка as i suppose find the same separator,thanks)

fn parse_three<T: FromStr>(s : &str, separator :char) -> Option<(T,T,T)>{
    match s.find(separator){
        None => None,
        Some(index) => {
            match s[index+1..].find(separator){//1ая ветка
                None => match (T::from_str(&s[..index]), T::from_str(&s[index+1..])){
                    (Ok(_l),Ok(_r)) => None,  //Some((l, r,None)),
                    _ => None
                },
                Some(indexx) =>{//вторая ветка
                    match (T::from_str(&s[..index]), T::from_str(&s[index+1..indexx]),T::from_str(&s[indexx+1..])){
                        (Ok(l),Ok(r),Ok(c)) => 
                            Some((l, r,c)),
                        _ => None
                    }
                }
            }
        }
    }
}

Also question:if i want new value

let dx = (domain.1-domain.0)/steps

where dsteps is usize, and numerator isize maybe, or f32 ... Compiler has complained,but logically all is right,i want to divide on number)

Here is the main problem with your code:

match s[index+1..].find(separator){//1ая ветка
// ...
  Some(indexx) =>{//вторая ветка
    match (
      T::from_str(&s[..index]), 
      T::from_str(&s[index+1..indexx]),
      T::from_str(&s[indexx+1..])) { /* ... */

indexx is the offset in &s[index+1..], not s. That is to say:

(&s[index+1..])[indexx] == s[index + 1 + indexx]

Also, this only works for characters that are 1 byte. Some characters have 2, 3, or 4 bytes.

assert!(','.len_utf8() == 1);
assert!('⚠'.len_utf8() == 3);

Here is a corrected version on the playground.

1 Like

You probably want something like:

let dx: isize = (domain.1 - domain.0) as isize / steps as isize;
// or
let dx: f32 = (domain.1 - domain.0) / steps as f32;
// etc
1 Like

I believe the original parse_two function is from the O'Reilly book Programming Rust, and also contains this bug. I don't see it mentioned on the errata page though. Is there someone on the forum with a copy of the book who can submit the error to O'Reilly? (Or just let me know the page number, product version, and format, and I can submit it.)

1 Like

Page 42, Jim Blendy author)

1 Like

I maybe wanted to use unused range in usize somehow,if maybe i will define it more than isize)) Also, do you know, this language i love very much, is it elaboration of MIT, or who is engaged in it?)

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.