I'm trying to verify an IP address in Rust, but I can't find a solution casting a str into a u8 that doesn't involve using nightly Rust:
use std::net::{IpAddr, Ipv4Addr};
fn verify_address(address: String) -> bool {
let v: Vec<&str> = address.split('.').collect();
let v_u8: Vec<u8> = v.iter().map(|c| *c.to_owned() as u8).collect();
let addr = IpAddr::V4(Ipv4Addr::new(v_u8[0], v_u8[1], v_u8[2], v_u8[3]));
//.expect("ERR: Error parsing IPv4 address!");
if !addr.is_ipv4() {
return false;
}
return true;
}
error[E0605]: non-primitive cast: `str` as `u8`
--> src/lib.rs:6:42
|
6 | let v_u8: Vec<u8> = v.iter().map(|c| *c.to_owned() as u8).collect();
| ^^^^^^^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait