rust/docs/user/assists.md
2019-10-25 23:38:15 +03:00

1.9 KiB

Assists

add_derive

Adds a new #[derive()] clause to a struct or enum.

// BEFORE
struct Point {
    x: u32,
    y: u32,<|>
}

// AFTER
#[derive()]
struct Point {
    x: u32,
    y: u32,
}

add_explicit_type

Specify type for a let binding

// BEFORE
fn main() {
    let x<|> = 92;
}

// AFTER
fn main() {
    let x: i32 = 92;
}

add_impl

Adds a new inherent impl for a type

// BEFORE
struct Ctx<T: Clone> {
     data: T,<|>
}

// AFTER
struct Ctx<T: Clone> {
     data: T,
}

impl<T: Clone> Ctx<T> {

}

add_impl_default_members

Adds scaffold for overriding default impl members

// BEFORE
trait T {
    Type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl T for () {
    Type X = ();
    fn foo(&self) {}<|>

}

// AFTER
trait T {
    Type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl T for () {
    Type X = ();
    fn foo(&self) {}
    fn bar(&self) {}

}

add_impl_missing_members

Adds scaffold for required impl members

// BEFORE
trait T {
    Type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl T for () {<|>

}

// AFTER
trait T {
    Type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl T for () {
    fn foo(&self) { unimplemented!() }

}

apply_demorgan

Apply De Morgan's law. This transforms expressions of the form !l || !r into !(l && r). This also works with &&. This assist can only be applied with the cursor on either || or &&, with both operands being a negation of some kind. This means something of the form !x or x != y.

// BEFORE
fn main() {
    if x != 4 ||<|> !y {}
}

// AFTER
fn main() {
    if !(x == 4 && y) {}
}

convert_to_guarded_return

Replace a large conditional with a guarded return.

// BEFORE
fn main() {
    <|>if cond {
        foo();
        bar();
    }
}

// AFTER
fn main() {
    if !cond {
        return;
    }
    foo();
    bar();
}