Preheat the oven to 350°F (175°C) and line a cupcake tray with liners.
In a large bowl, beat the sugar and oil together, then add the eggs one at a time, beating well after each addition.
In another bowl, whisk together the flour, baking soda, cinnamon, and salt. Gradually add this to the wet ingredients, stirring gently until just combined.
Fold in the grated carrots and walnuts, if using.
Fill the cupcake liners about two-thirds full with the batter.
Bake for 18-20 minutes, or until a toothpick inserted into the center comes out clean.
Allow to cool in the pan for 5 minutes, then transfer to a wire rack to cool completely before frosting.