A generalized herbivore gut is modelled as (i) a well-stirred anterior chamber in which microbial fermentation occurs; (ii) a tubular reactor in which digestion but no fermentation occurs; and (iii) a posterior fermentation chamber. The rate at which the herbivore gains metabolizable energy is calculated for diets that can be eaten at different rates and contain different energy densities of easily digested cell contents, and of cell wall materials that can be fermented but not digested. The optimum gut structure for each diet is determined. Chewing probably speeds digestion and fermentation but reduces eating time. Optimal chewing times are determined for particular diets and guts. Herbivores often have a choice between poorer food that can be eaten fast and richer food that can only be eaten more slowly. Energy costs may be incurred in travelling between patches of the richer food. Optimal diet choices are predicted for herbivores with particular gut structures.