Medics in Vietnam, and this occurred in earlier wars, routinely carried the candy M&Ms in order to give a placebo to a soldier who was mortally wounded in the hope that the soldier would believe that he was going to receive further lifesaving treatment. The M&M was simply part of the medic's tool kit to save lives or to provide comfort when there was no hope of saving a life.
Although most wounds received by a soldier in Vietnam were survivable because definitive medical care could be given quickly--many times, within 15 or 20 minutes of evacuation by helicopter--there were wounds in which there was no precedent for survival under any conditions. In other words, the wounded soldier was going to die within minutes because there was not enough time or sufficient medical care to save him. In the hopes of giving a dying man the hope of surviving, medics would often use something like an M&M to make the wounded man believe he could survive. There were also cases in which a wounded man who was making a lot of noise, which might endanger the unit, would be given an M&M and told it was a painkiller in the hopes of quietening the soldier.
Sometimes, if a soldier had a wound that affected the central nervous system (possible trauma to the brain), the medic could not give morphine because it might exacerbate the effects of the wound, so the medic would give a placebo (sometimes an M&M), hoping that the placebo effect would lessen the pain.
For many combat medics, the M&M was a mixed blessing--it might provide some comfort to the dying but it also meant that the medic had no way to save the wounded man.