2

Rust allows explicit type definition at variable declaration (although it seems that such syntax is rarely used by some reasons). E.g. such declarations are valid:

let number: i32 = 10;
let (tuple_value1, tuple_value2): (i32, String) = (10, String::from("Text"));
let (numeric_value, bytes): (i32, &[u8]) = (10, tuple_value2.as_bytes());

But how variable types can be explicitly defined in the for loop? I'm currently studying The Rust Programming Language book's chapter 4.3. I'm trying to apply type definition at variables declaration in the for loop in listing 4-10:

fn first_word(s: &String) -> usize {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return i;
        }
    }

    s.len()
}

Nevertheless when trying to supply mentioned listing's for loop with the type definition

fn first_word(s: &String) -> usize {
    let bytes: &[u8] = s.as_bytes();

    for (i, item): (usize, std::iter::Enumerate<std::slice::Iter<u8>>) in bytes.iter().enumerate() {
        if item == b' ' {
            return i;
        }
    }

    s.len()
}

I get:

error: expected `in`, found `:`
 --> src/main.rs:7:15
  |
7 |     for (i, item): (usize, &[u8]) in bytes.iter().enumerate() {
  |                  ^ expected `in` here

error[E0308]: mismatched types
  --> src/main.rs:4:36
   |
4  |   fn first_word(s: &String) -> usize {
   |  ____________________________________^
5  | |  let bytes: &[u8] = s.as_bytes();
6  | |
7  | |  for (i, item): (usize, &[u8]) in bytes.iter().enumerate() {
...  |
13 | |  s.len()
14 | | }
   | |_^ expected usize, found ()
   |
   = note: expected type `usize`
              found type `()`

error: aborting due to 2 previous errors

error: Could not compile `hello_world`.

So far the book doesn't contain a description of how to perform an explicit type definition in the loops. So did for me googling. My question is about the proper type definition syntax in the for loop (if such exists, I'm strongly hope so).

Cryptor
  • 347
  • 1
  • 3
  • 9
  • 3
    [Related RFC issue](https://github.com/rust-lang/rfcs/issues/354) – Lukas Kalbertodt Nov 19 '17 at 15:14
  • 2
    @Shepmaster I do not think https://stackoverflow.com/q/24463655/7496656 is a real duplicate as even though the reason for both questions are the same, the answers will be and are different. Even nearer duplicate but still not quite: https://stackoverflow.com/q/34304341/7496656 Do you think that should also be marked as a duplicate? – Jan Zerebecki Nov 19 '17 at 16:02
  • 1
    I moved [my answer](https://stackoverflow.com/a/47379212/1870153) to the duplicate. – ljedrz Nov 19 '17 at 16:36
  • @JanZerebecki the question on both is "can I have an explicit type" and the answer to both is "no". Workarounds exist, and the answer that was here *also* applies to that one. – Shepmaster Nov 19 '17 at 16:38
  • 1
    *although it seems that such syntax is rarely used by some reasons* — yes, we programmers are lazy and hate having to type redundant information. Rust has a *very* powerful type inference engine that works almost all the time. In this case, it's pointless to specify the types because you can't change them — they are what they are. – Shepmaster Nov 19 '17 at 16:40
  • @Shepmaster, type definition is not a redundant information for every programmer. For me it's useful at least for two reasons: • It's more easy to read a code which has type definitions, you're more easily get understanding of what's going at the particular part of code. • Errors related to mismatched types will appear at the very source and not somewhere down the code. – Cryptor Nov 19 '17 at 17:01
  • @Cryptor I'd strongly encourage you to honestly give Rust a shot while removing *every* redundant type annotation. I favor shorter functions and since functions must have type declarations, those provide concrete points of reference. Also, Rust's error messages are generally superb, pointing exactly where they need to. You may simply have experience with other languages that don't have error messages of this caliber. Besides, I'm just explaining why most Rust code doesn't use the explicit types, you can do whatever you want. – Shepmaster Nov 19 '17 at 17:05

0 Answers0