Skip to content

Conversation

woodruffw
Copy link
Contributor

Copying some context from a conversation in the Rust discord:

  • Both vec![T; 0] and [T; 0] are syntactically valid, and produce empty containers of their respective types

  • Both also have side effects:

fn side_effect() -> String {
    println!("side effect!");

    "foo".into()
}

fn main() {
    println!("before!");

    let x = vec![side_effect(); 0];
    
    let y = [side_effect(); 0];

    println!("{:?}, {:?}", x, y);
}

produces:

before!
side effect!
side effect!
[], []

This PR just adds two small notes to each's documentation, warning users that side effects can occur.

I've also submitted a clippy proposal: rust-lang/rust-clippy#6439

@rust-highfive
Copy link
Contributor

r? @dtolnay

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 10, 2020
@jyn514 jyn514 added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Dec 11, 2020
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay
Copy link
Member

dtolnay commented Dec 13, 2020

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Dec 13, 2020

📌 Commit d986924 has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 13, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 14, 2020
…side-effects, r=dtolnay

doc(array,vec): add notes about side effects when empty-initializing

Copying some context from a conversation in the Rust discord:

* Both `vec![T; 0]` and `[T; 0]` are syntactically valid, and produce empty containers of their respective types

* Both *also* have side effects:

```rust
fn side_effect() -> String {
    println!("side effect!");

    "foo".into()
}

fn main() {
    println!("before!");

    let x = vec![side_effect(); 0];

    let y = [side_effect(); 0];

    println!("{:?}, {:?}", x, y);
}
```

produces:

```
before!
side effect!
side effect!
[], []
```

This PR just adds two small notes to each's documentation, warning users that side effects can occur.

I've also submitted a clippy proposal: rust-lang/rust-clippy#6439
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 14, 2020
…laumeGomez

Rollup of 3 pull requests

Successful merges:

 - rust-lang#79918 (doc(array,vec): add notes about side effects when empty-initializing)
 - rust-lang#79936 (Fix item name display on mobile)
 - rust-lang#80013 (Refactor test_lang_string_parse to make it clearer)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 5d8b2a5 into rust-lang:master Dec 14, 2020
@rustbot rustbot added this to the 1.50.0 milestone Dec 14, 2020
@woodruffw woodruffw deleted the ww/doc-initializer-side-effects branch December 23, 2022 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants