Skip to content

Mutable borrow of unit-like struct is a weird edge case #11267

Closed
@comex

Description

@comex
// We need a mutable borrow to an empty struct because it implements a trait
// that takes &mut self.
struct Empty;
impl Iterator<int> for Empty {
    fn next(&mut self) -> Option<int> { None }
}

fn do_something_with(a : &mut Iterator<int>) {
    println!("{}", a.next())
}

fn main() {
    // This is OK...
    let mut a = Empty;
    do_something_with(&mut a);
    // But if I want to use an immediate expression, as is possible with
    // non-empty structs...
    // "error: cannot borrow immutable static item as mutable"
    do_something_with(&mut Empty);
}

It seems like it would make sense to allow mutably borrowing the Empty singleton.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions