I entered two string literals and converted them into arrays of bytes and now I'm trying to compare both arrays bytes to check for a match. If they match successfully, I want to save it into an unknown size array find
, but it is not saving:
fn main() {
let mut m: u8 = 0;
// enter the first_name
let alpha = "fawad";
//covert name1 into array bytes
let name1 = alpha.as_bytes();
// enter the second_name
let input = String::new();
//covert name2 into array bytes
let name2 = input.as_bytes();
//want to create an unknown size of an array
let find: Box<[u8]>;
for (i, &place1) in name1.iter().enumerate() {
let place1 = name1[i];
for (j, &place2) in name2.iter().enumerate() {
let place2 = name2[j];
if place1 == place2 {
let find = place2;
m = m + 1;
} else {
break;
}
}
}
println!("comparison shows {:?}", find);
}
I get:
error: expected one of `:`, `;`, `=`, or `@`, found `[`
--> src\main.rs:17:13
|
17 | let find[] : Box<[u8]>;
| ^ expected one of `:`, `;`, `=`, or `@` here
The compiler message is not reproducable, yet your code reveals some misunderstandings. Here some hints how you could change it:
You want an array of unknown size (and it shall probably contain the bytes already matched, so it must be mutable):
let find : Box<[u8]>;
by let mut find = Vec::new();
You want to iterate the bytes of name1 and name2
for (i, &place1) in name1.iter().enumerate()
by for &place1 in name1.iter()
for (i, &place2) in name2.iter().enumerate()
by for &place2 in name2.iter()
let variable = ...
shadows all former definitions of variable
. You do not want to shadow the variables place1
, place2
you want to use these variables.
let place1 = name1[i];
let place2 = name2[j];
You want to add the matching byte to the array find
. Your code will again shadow the definition of find. So
let find = place2;
by find.push(place2);