Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few doc examples. #239

Merged
merged 4 commits into from Nov 14, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -234,6 +234,26 @@ impl Url {
}

/// Parse a string as an URL, with this URL as the base URL.
///
/// # Examples
///
/// ```rust
/// use url::Url;
///
/// let url = Url::parse("https://example.net").unwrap();
/// let url = url.join("foo").unwrap();
/// assert_eq!(url.as_str(), "https://example.net/foo");

This comment has been minimized.

@SimonSapin

SimonSapin Nov 14, 2016

Member

Please change this to showcase trailing slashes: https://example.net/foo joined with bar is https://example.net/bar, but https://example.net/foo/ joined with bar is https://example.net/foo/bar

r=me with this.

/// ```
///
/// Trailing slashes are not preserved:
///
/// ```rust
/// use url::Url;
///
/// let url = Url::parse("https://example.net/foo/").unwrap();
/// let url = url.join("bar").unwrap();
/// assert_eq!(url.as_str(), "https://example.net/foo/bar");
/// ```
#[inline]
pub fn join(&self, input: &str) -> Result<Url, ::ParseError> {
Url::options().base_url(Some(self)).parse(input)
@@ -251,6 +271,16 @@ impl Url {
/// Return the serialization of this URL.
///
/// This is fast since that serialization is already stored in the `Url` struct.
///
/// # Examples
///
/// ```rust
/// use url::Url;
///
/// let url_str = "https://example.net/";
/// let url = Url::parse(url_str).unwrap();
/// assert_eq!(url.as_str(), url_str);
/// ```
#[inline]
pub fn as_str(&self) -> &str {
&self.serialization
@@ -259,6 +289,16 @@ impl Url {
/// Return the serialization of this URL.
///
/// This consumes the `Url` and takes ownership of the `String` stored in it.
///
/// # Examples
///
/// ```rust
/// use url::Url;
///
/// let url_str = "https://example.net/";
/// let url = Url::parse(url_str).unwrap();
/// assert_eq!(url.into_string(), url_str);
/// ```
#[inline]
pub fn into_string(self) -> String {
self.serialization
@@ -890,6 +930,20 @@ impl Url {
///
/// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
/// do nothing and return `Err`.
///
/// # Examples
///
/// ```
/// use url::Url;
///
/// let mut url = Url::parse("ssh://example.net:2048/").unwrap();
///
/// url.set_port(Some(4096)).unwrap();
/// assert_eq!(url.as_str(), "ssh://example.net:4096/");
///
/// url.set_port(None).unwrap();
/// assert_eq!(url.as_str(), "ssh://example.net/");
/// ```
pub fn set_port(&mut self, mut port: Option<u16>) -> Result<(), ()> {
if !self.has_host() || self.scheme() == "file" {
return Err(())
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.