url::Url fails to read String reference

hi ...

Some assistance needed :slight_smile:
I am trying to use the following function:

https://docs.rs/url/1.7.2/src/url/lib.rs.html#303-305

/// Parse an absolute URL from a string.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use url::Url;
    /// # use url::ParseError;
    ///
    /// # fn run() -> Result<(), ParseError> {
    /// let url = Url::parse("https://example.net")?;
    /// # Ok(())
    /// # }
    /// # run().unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// If the function can not parse an absolute URL from the given string,
    /// a [`ParseError`] variant will be returned.
    ///
    /// [`ParseError`]: enum.ParseError.html
    #[inline]
    pub fn parse(input: &str) -> Result<Url, ::ParseError> {
        Url::options().parse(input)
    }

if I use it as :

fn main(){
    let url = "https://bla/bla";
    let x = Url::parse(url).unwrap();
}

No problem. But if I have a string:

fn main(){
    let url = "https://bla/bla".to_string();
    let x = Url::parse(&url).unwrap();
}

i get:

thread 'Map' panicked at 'called `Result::unwrap()` on an `Err` value: RelativeUrlWithoutBase', src/libcore/result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Any help ?

Your last example works in playground. Could you please provide some more details?

1 Like

ah sorry .. it does because playgroung has 2.0.0 of Url but, I am playing with 1.7.2 version since ffsend_api v0.3.2 requires Url 1.7.2 and complains if I use 2.0.0.

https://docs.rs/ffsend-api/0.3.2/src/ffsend_api/file/remote_file.rs.html#120-151

No, that's not it. Here is a test with Url 1.7.2.

It is impossible that the problem is as you are seeing it. &String -> &str is a coercion handled by the language. Your two code snippets should have provably identical behavior.

1 Like

firast of all... thank you so much!! ... i guess I am too tired ... it is a complex setup so I naively assumed ... sorry for wasting time !!

UPDATE:

It was
parsing from json Value:

assert_eq!( data["url"], "\"http://bla/bla\"" );  // True
//not
assert_eq!( data["url"], "http://bla/bla" );  // False

sorry once more !

1 Like