No method named `iter` found for type `usize` in the current scope


#1

here is my code

fn main() {
    println!("Hello, world!");
    let m :String = String::from("Hello world");
    let mut x :usize = ret_s(&m);

    let slice  = &m[..x];
    println!("slice ======  {}",slice);
}

fn ret_s(x: &str) -> usize {
	let m = x.as_bytes();
	let mut m :usize;
	for (i,&item) in m.iter().enumerate() {
		  if item == b' ' {
		  	 m = i;
		  }
		}

		return m ;
}

error message is

C:\Users\Dell\Desktop\rust\learn\string>cargo build
   Compiling string v0.1.0 (file:///C:/Users/Dell/Desktop/rust/learn/string)
error[E0599]: no method named `iter` found for type `usize` in the current scope
  --> src\main.rs:13:21
   |
13 |     for (i,&item) in m.iter().enumerate() {
   |                        ^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: Could not compile `string`.

To learn more, run the command again with --verbose.

#2

You redefined m as a usize in the second line of that function.


#3

so i should remove that type defination ?


#4

No, because then you won’t have anywhere to store that value. Just use a different name. It’s alright; you aren’t being charged per variable.


#5

thanks