-3

I have below rust code.

let mut https: Vec<u8>= Vec::new();
https.push(b'/');

When I am running cargo clippy, I am getting below warning

warning: calls to `push` immediately after creation
  https.push(b'/');
                  ^ help: consider using the `vec![]` macro: `let mut https: Vec<u8> = vec![..];`

Can somebody please help me to remove this warning?

Stargateur
  • 24,473
  • 8
  • 65
  • 91
Ayush Mishra
  • 567
  • 1
  • 7
  • 19
  • 5
    As the compiler suggests, the warning can be removed this way: `let mut https: Vec= vec![b'/'];`. – Joe_Jingyu Dec 03 '21 at 04:33
  • Relevant section from the book: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html#storing-lists-of-values-with-vectors – E_net4 Dec 03 '21 at 08:23
  • 2
    The downvote was probably because the error message literally tells you how to solve it. – Thomas Dec 03 '21 at 08:40
  • Putting aside the compiler suggests you how to solve it, this is also a duplicate (of two questions!) I'd expect the OP to research before asking. – Chayim Friedman Dec 03 '21 at 10:34

1 Answers1

1

Clippy is advising that instead of pushing the new value manually you use the vec![] macro to construct a vector with the given items:

let https: Vec<u8> = vec![b'/'];
Netwave
  • 40,134
  • 6
  • 50
  • 93