Skip to content

str::view interacts strangely with @str values #3572

Closed
@Dretch

Description

@Dretch

I was looking into a way to fix #3433 when I discovered the following issue:

fn main(){
    let up_here = @"abcdef";
    io::println(str::view(up_here, 1, 5).to_unique());   // prints bcde
    io::println(str::view(@"abcdef", 1, 5).to_unique()); // should print bcde but instead prints e
}

Interestingly rustc rejects the erroneous println when a ~str is used instead:

    io::println(str::view(~"abcdef", 1, 5).to_unique());

Because:

test-atstr-view.rs:6:30: 6:39 error: illegal borrow: borrowed value does not live long enough
test-atstr-view.rs:6         io::println(str::view(~"abcdef", 1, 5).to_unique()); // should print bcde but instead prints e
                                                   ^~~~~~~~~
test-atstr-view.rs:6:20: 6:59 note: borrowed pointer must be valid for the call at 6:20...
test-atstr-view.rs:6         io::println(str::view(~"abcdef", 1, 5).to_unique()); // should print bcde but instead prints e
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test-atstr-view.rs:6:20: 6:47 note: ...but borrowed value is only valid for the call at 6:20
test-atstr-view.rs:6         io::println(str::view(~"abcdef", 1, 5).to_unique()); // should print bcde but instead prints e
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions