rust/docs/user/assists.md
2019-10-26 21:22:40 +03:00

3.2 KiB

Assists

Cursor position or selection is signified by character.

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) {}
}

change_visibility

Adds or changes existing visibility specifier.

// BEFORE
fn frobnicate() {}

// AFTER
pub(crate) fn frobnicate() {}

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();
}

fill_match_arms

Adds missing clauses to a match expression.

// BEFORE
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        
    }
}

// AFTER
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => (),
        Action::Stop => (),
    }
}

flip_binexpr

Flips operands of a binary expression.

// BEFORE
fn main() {
    let _ = 90 + 2;
}

// AFTER
fn main() {
    let _ = 2 + 90;
}

flip_comma

Flips two comma-separated items.

// BEFORE
fn main() {
    ((1, 2), (3, 4));
}

// AFTER
fn main() {
    ((3, 4), (1, 2));
}

inline_local_variable

Inlines local variable.

// BEFORE
fn main() {
    let x = 1 + 2;
    x * 4;
}

// AFTER
fn main() {
    (1 + 2) * 4;
}

introduce_variable

Extracts subexpression into a variable.

// BEFORE
fn main() {
    (1 + 2) * 4;
}

// AFTER
fn main() {
    let var_name = (1 + 2);
    var_name * 4;
}