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

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.

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

so i should remove that type defination ?

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.

2 Likes

thanks

2 posts were split to a new topic: Why does variable n need initialized?