Uber is throwing in the towel in China, agreeing to be bought out by its biggest rival in the country, Didi Chuxing, in exchange for a 20 percent share in a company reorganized under the Didi banner.

The deal, reported late Sunday, ends the ride-hailing giant’s aggressive effort to muscle into China, which had cost Uber more than $2 billion as it sought to recruit drivers and gain market share in the most populous country in the world.

In addition to the challenges inherent in operating in China, Uber faced a formidable foe in Didi Chuxing, a homegrown company that itself had drawn considerable domestic and foreign investment, including $1 billion earlier this year from Apple.

Despite Uber’s inroads in China, Didi has remained the dominant actor in the ride-hailing industry, reportedly providing 85 percent of app-requested rides in the country.

Didi’s valuation is expected to reach $35 billion after deal, with Uber’s stake worth an estimated $7 billion. Uber CEO Travis Kalanick will reportedly have a seat on the board of the new company.

Didi is also expected to make a $1 billion investment in Uber Global to support its worldwide expansion efforts; Uber has been valued $62.5 billion.

Kalanick had defied the experience of other Silicon Valley tech companies that been deterred by China’s challenging governmental and economic climate.  But in a statement shared with several news outlets, Kalanick said the China venture had proven too costly.

“However, as an entrepreneur, I’ve learned that being successful is about listening to your head as well as following your heart,” he said. “Uber and Didi Chuxing are investing billions of dollars in China and both have yet to turn a profit there.”

The deal comes just days after the Chinese government enacted a law that officially made ride-hailing legal in the country, while establishing a regulatory system for the industry.

Some observers compared the deal to Yahoo!’s decision in 2005 to buy a stake in the Alibaba, the Chinese internet services giant, rather than compete directly against it; that stake is now valued at close to $30 billion and is considered one of Yahoo!’s shrewder decisions.

Uber’s investors had reportedly been pushing for it to cut a deal in China and turn its attention to more promising overseas markets, such as Latin America and India. The deal may also allow the fiercely competitive Uber to go harder at its main domestic rival, Lyft.

The Chinese government must still approve the deal.