mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-26 13:30:24 +00:00
Update net.md
This commit is contained in:
parent
ba4f7b30ff
commit
f483052b0c
1 changed files with 30 additions and 20 deletions
28
src/net.md
28
src/net.md
|
@ -22,9 +22,10 @@ use url::Url;
|
|||
|
||||
fn main() {
|
||||
let url_string = "data:text/plain,Hello?World#";
|
||||
let url = Url::parse(url_string).unwrap();
|
||||
|
||||
assert_eq!(url.as_str(), url_string)
|
||||
match Url::parse(url_string) {
|
||||
Ok(url) => println!("Url was succesfully parsed"),
|
||||
Err(error) => println!("Invalid url syntax")
|
||||
}
|
||||
}
|
||||
```
|
||||
For documentation of `ParseError` see [this](https://docs.rs/url/enum.ParseError.html).
|
||||
|
@ -43,13 +44,20 @@ The [`Url`](https://docs.rs/url/struct.Url.html) struct provides the [`join`](ht
|
|||
|
||||
```rust
|
||||
extern crate url;
|
||||
use url::Url;
|
||||
use url::{Url, ParseError};
|
||||
|
||||
fn main() {
|
||||
let this_document = Url::parse("http://servo.github.io/rust-url/url/index.html").unwrap();
|
||||
let css_url = this_document.join("../main.css").unwrap();
|
||||
match create_from_base("http://servo.github.io/rust-url/url/index.html", "../main.css") {
|
||||
Ok(url) => assert_eq!(url.as_str(), "http://servo.github.io/rust-url/main.css"),
|
||||
Err(error) => println!("Invalid url syntax")
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css")
|
||||
fn create_from_base(base: &str, seg: &str) -> Result<Url, ParseError> {
|
||||
let base_url = Url::parse(base)?;
|
||||
let seg_url = base_url.join(seg)?;
|
||||
|
||||
Ok(seg_url)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -63,10 +71,11 @@ extern crate url;
|
|||
use url::{Url, Host};
|
||||
|
||||
fn main() {
|
||||
let url = Url::parse("ftp://example.com/foo").unwrap();
|
||||
if let Ok(url) = Url::parse("ftp://example.com/foo") {
|
||||
assert!(url.scheme() == "ftp");
|
||||
assert!(url.host() == Some(Host::Domain("example.com".into())));
|
||||
assert!(url.port_or_known_default() == Some(21));
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -77,7 +86,7 @@ extern crate url;
|
|||
use url::{Url, Origin, Host};
|
||||
|
||||
fn main() {
|
||||
let url = Url::parse("ftp://example.com/foo").unwrap();
|
||||
if let Ok(url) = Url::parse("ftp://example.com/foo") {
|
||||
assert_eq!(
|
||||
url.origin(),
|
||||
Origin::Tuple(
|
||||
|
@ -86,6 +95,7 @@ fn main() {
|
|||
21
|
||||
)
|
||||
);
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue