0
Is it possible to write Rust code that is as compact and expressive as Python?
For example, consider a simple web crawler using the spider library in Rust that extracts all links from a website with just a few lines of code: Description: Here’s an example Rust code snippet using the spider crate: use spider::tokio; use spider::website::Website; #[tokio::main] async fn main() { let mut website: Website = Website::new("https://spider.cloud"); website.crawl().await; let links = website.get_links(); for link in links { println!("- {:?}", link.as_ref()); } } With just this code, you can crawl a website and extract all the links on the server. The question is whether Rust can achieve the same level of conciseness and expressiveness as Python for tasks like this.
1 Réponse
+ 3
# yes it's a good idea to learn Rust. it's an awesome language. but Rust is not about compact code. it's about safe and fast code.
# use Rust where it actually have the technical advantage. maybe for rapidly processing the 'huge' data you already gathered.
# but webcrawling is not the case where it's decisively better than other languages. the speed is limited by slow operations like network requests rather than the speed of the CPU processing the data.
# this Python code will get you the result faster than the amount of time you need to compile your Rust code:
import requests
from bs4 import BeautifulSoup
url = 'https://spider.cloud'
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))