String assertion failed error

When making a small project I encontered a run time error:

thread 'main' panicked at 'assertion failed: self.is_char_boundary(n)', /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\alloc\src\string.rs:1728:29

is it a compiler/libstd error? and if it is how should I report it?

Here's the backtrace:

thread 'main' panicked at 'assertion failed: self.is_char_boundary(n)', /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\alloc\src\string.rs:1728:29
stack backtrace:
   0: rust_begin_unwind
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src/panicking.rs:107:14
   2: core::panicking::panic
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src/panicking.rs:48:5
   3: alloc::string::String::replace_range
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\alloc\src/string.rs:1728:29
   4: paste_rs_cli::api::extract_paste_id
             at .\src\api.rs:117:5
   5: paste_rs_cli::api::Paste::from
             at .\src\api.rs:30:22
   6: paste_rs_cli::main::{{closure}}
             at .\src\main.rs:96:19
   7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\future/mod.rs:80:19
   8: tokio::park::thread::CachedParkThread::block_on::{{closure}}
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\park\thread.rs:263:54
   9: tokio::coop::with_budget::{{closure}}
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\coop.rs:102:9  10: std::thread::local::LocalKey<T>::try_with
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\std\src\thread/local.rs:399:16
  11: std::thread::local::LocalKey<T>::with
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\std\src\thread/local.rs:375:9
  12: tokio::coop::with_budget
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\coop.rs:95:5
  13: tokio::coop::budget
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\coop.rs:72:5
  14: tokio::park::thread::CachedParkThread::block_on
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\park\thread.rs:263:31
  15: tokio::runtime::enter::Enter::block_on
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\runtime\enter.rs:151:13
  16: tokio::runtime::thread_pool::ThreadPool::block_on
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\runtime\thread_pool\mod.rs:77:9
  17: tokio::runtime::Runtime::block_on
             at C:\Users\shatn\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.15.0\src\runtime\mod.rs:463:43
  18: paste_rs_cli::main
             at .\src\main.rs:102:5
  19: core::ops::function::FnOnce::call_once
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `C:\Users\shatn\code\paste_rs\target\debug\paste_rs-cli.exe` (exit code: 101)

here's main:

// main.rs

use crate::api::Paste;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let _paste1 = Paste::from("osx")?;
    let _paste2 = Paste::from("https://paste.rs/osx")?;
    let _paste3 = Paste::from("paste.rs/osx")?;

    // dbg!(paste1);
    // dbg!(paste2);
    // dbg!(paste3);

    Ok(())
}
// api.rs

pub struct Paste(String);

impl Paste {
    /// Make a new paste struct from a string(Url, incomplete url, id)
    ///
    /// # Example
    /// ```
    ///
    pub fn from(val: &str) -> anyhow::Result<Self> {
        if is_url(val) && is_paste_rs_url(val) {
            Ok(Paste(extract_paste_id(&val.to_string())))
        } else if !is_url(val) && is_paste_rs_url(val) {
            let full_url = format!("https://{}", val);
            Ok(Paste(extract_paste_id(&full_url.to_string())))
        } else if val.len() == 3 {
            Ok(Paste(val.to_string()))
        } else if is_url(val) && !is_paste_rs_url(val) {
            bail!("Invalid URL")
        } else {
            bail!("Invalid argument")
        }
    }

/*  ... */
}

I'm compiling with rustc 1.58.1 on x86_64-pc-windows-gnu

It seems extract_paste_id is splitting the string given to it in the middle of a character, could you share the body of that function?

1 Like

Here:

const PASTE_RS_URL: &str = "https://paste.rs/";

fn extract_paste_id(url: &String) -> String {
    let mut url = url.to_owned();
    url.replace_range(0..PASTE_RS_URL.len(), "");
    url
}

I have changed the implementation of that function and the error is gone

here's the new function:

fn extract_paste_id(url: &String) -> String {
    let url = url.to_owned();
    // url.replace_range(0..PASTE_RS_URL.len(), "");
    url.replace(PASTE_RS_URL, "")
}

sorry for taking up your time

It's cool, don't worry :slight_smile: Glad you got it working

If you're dealing with URLs, may I suggest the url crate?

thanks, I'll look into it

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.