Brazil is quite a poor country, and in Rio it's easy to get into trouble if you don't keep aware of your surroundings. We had few problems ourselves and even took a tour of a local Favela. Favela's are basically illegal neighbourhoods with no authorized electricity, plumbing, or phones, yet a majority of the population lives.
While the city was younger with fewer people, the government decided to turn all of the mountain sides into official parks, so that no one could build there. The mountains were to be used for everyone's enjoyment, not just the rich. With little land nestled between the steep mountains I imagine the city quickly ran out of affordable places for middle and lower class people to live. So they started building up the mountain sides into the park land.
Rio is probably one of the only places in the world where the poorest people have the nicest views. In North America, a favela might be referred to as a shanty town. All electricity and phone lines are illegal, there's no proper plumbing, and no proper sewage or garbage removal. The houses are slightly better than the corrugated tin houses in most third world ghettos, but not too much better. The houses have limited foundation, and when you want more people or more family to live with you, there's nowhere to build but up. During heavy rains buildings often collapse and slide down the mountain side.
The other difference between your standard third world squatter camp and favela's is the quality of life. The favela's in Rio actually house many lower-middle class families, not just the poorest. These are full on communities with everything from grocery shopping, cell phone sales, clothes, butchers, bakers, everything you'd normally find in a regular city or town. Housing is simply far too expensive in Rio for the average Brazilian.
They're also supposedly very safe if you live there... and very dangerous if you don't. Each favela has a single gang that runs all of the drug operations in that community. The gangs make up a large proportion of the younger people living there. Each favela has people on the lookout for unfamiliar faces. Someone suspicious will be shot on the spot. The gangs are very paranoid of undercover police, spies from neighbouring favela gangs, and journalists who may take photos of gang members. After they've killed you, they'll check your ID and plant drugs on the innocent so the police won't investigate ("a drug related homicide").
Both Sam and I felt a bit odd on the tour we took of one (since we were with a local, it was "safe"). It seemed a bit invasive to their community. But it really was an eye opener.