Infinitives (to+V) and gerunds (V-ing) can act as nouns and therefore can be used as subjects or objects in sentences.
Both can be used as subjects. Using the gerund is more usual. To use the infinitive is very formal and is more often found in formal writing than in spoken everyday language.
Both can be used as objects but the choice in that case does not depend on register but on what verb they depend on. Dictionaries and/or grammar books usually tell what to use with what verbs. For example:
- I enjoy swimming.
- I want to swim across the river.
- I like running.
- I like to run.
Here is an extensive lesson on how to use infinitives and gerunds as subjects and objects.