Hi everyone, i'm learning about rust, i created this small example like test.
This code is to decode a URI from String.
How to better this code?
trait Uri{
fn new( data : &mut String ) -> Self;
}
#[derive(Debug)]
struct Purl{
verb : String,
app : String,
res : String,
method : String,
qry : Vec,
}
impl Uri for Purl{
fn new( my_url : &mut String ) -> Purl{
let mut struc = Purl{
verb : String::new(),
app : String::new(),
res : String::new(),
method : String::new(),
qry : vec![ String::new() ]
};
let mut split : Vec<&str> = my_url.split(" ").collect();
struc.verb = split.remove(0).to_string();
let mut my_url : String = split.remove(0).to_string();
let mut split : Vec<&str> = my_url.split("/").collect();
struc.app = split.remove(1).to_string();
struc.res = split.remove(1).to_string();
my_url = split.remove(1).to_string();
let mut split : Vec<&str> = my_url.split("?").collect();
struc.method = split.remove(0).to_string();
my_url = split.remove(0).to_string();
let split : Vec<String> = my_url.split("&").map(|s| s.to_string() ).collect();
struc.qry = split;
struc
}
}
fn main() {
let mut my_url : String = "GET /clinica/pacientes/list?nome=almir&st=inativo&order=nome HTTP/1.1\r\n".to_string();
let struc : Purl = Purl::new( &mut my_url );
println!("{:?}", struc);
}