Question: In my current application for a company Im having a ForegroundService that is running while the workers are on duty (logged in to the app). 
It is important to keep that foreground service running while users are logged in. To recover from cases where this foreground service gets killed somehow (User task swipe accidently or System kill) ive implemented a periodic JOB with Jobscheduler which reactivates the ForegroundService in case it gets shut down. This technique works well pre Android 8 OREO. START\_STICKY and other techniques alone did not do the trick for me.

In Android 8, as soon as the foreground service gets killed, the periodic job gets killed as well. Im getting the notification in logcat that the job is not allowed to run. To my understanding, Jobs schould be able to run even when app is in background or killed. And its working on pre OREO devices the way it should.

To my knowledge, I can fix that by enable the option "autostart" in app settings. But since there is no way to know if employees tunred that on, its not a reliable thing as well.

So my questions:
- Why does the Job scheduler stops working as it should in Android 8? 
- Are there any other reliable techniques I could use to let my ForegroundService recover from shutdowns in ANDROID OREO? 

Ive read <https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c> but that did not answer my questions 

Thank you very much

Answer: 
A UTF-8 encoded string may contain characters which consists of multiple bytes. In your case, `п` starts at index 6 (inclusive) and ends at position 8 (exclusive) so indexing 7 is not the start of the character. This is why your error occurred.

You may use [`str::char_indices()`](https://doc.rust-lang.org/std/primitive.str.html#method.char_indices) for solving this (remember, that getting to a position in a UTF-8 string is `O(n)`):

```
fn get_utf8_slice(string: &str, start: usize, end: usize) -> Option<&str> {
    assert!(end >= start);
    string.char_indices().nth(start).and_then(|(start_pos, _)| {
        string[start_pos..]
            .char_indices()
            .nth(end - start - 1)
            .map(|(end_pos, _)| &string[start_pos..end_pos])
    })
}

```

[playground](https://play.rust-lang.org/?gist=8615dce4ec8a36cc1220b79d2af7751f&version=stable&mode=debug&edition=2015)

You may use [`str::chars()`](https://doc.rust-lang.org/std/primitive.str.html#method.chars) if you are fine with getting a `String`:

```
let string: String = text.chars().take(end).skip(start).collect();

```
Here is a function which retrieves a utf8 slice, with the following pros:

* handle all edge cases (empty input, 0-width output ranges, out-of-scope ranges);
* never panics;
* use start-inclusive, end-exclusive ranges.

```
pub fn utf8_slice(s: &str, start: usize, end: usize) -> Option<&str> {
    let mut iter = s.char_indices()
        .map(|(pos, _)| pos)
        .chain(Some(s.len()))
        .skip(start)
        .peekable();
    let start_pos = *iter.peek()?;
    for _ in start..end { iter.next(); }
    Some(&s[start_pos..*iter.peek()?])
}

```