-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.rs
40 lines (35 loc) · 1.01 KB
/
methods.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#[derive(Debug)]
struct URL {
protocol: String,
hostname: String,
pathname: String,
}
impl URL {
fn toString(&self) -> String {
format!("{}://{}{}", self.protocol, self.hostname, self.pathname)
}
fn from(url: &str) -> URL {
let string = String::from(url);
let vec: Vec<&str> = string.split("://").collect();
let protocol = String::from(vec[0]);
let rest = String::from(vec[1]);
let vec2: Vec<&str> = rest.split("/").collect();
let hostname = String::from(vec2[0]);
let pathname = String::from(vec2[1]);
return URL {
protocol,
hostname,
pathname,
};
}
}
fn main() {
let url = URL {
protocol: String::from("https"),
hostname: String::from("www.google.com"),
pathname: String::from("/search?q=hello"),
};
let url2 = URL::from("https://www.google.com/search?q=hello");
println!("url is {:?}", url.toString());
println!("url2 is {:?}", url2);
}