Rust Input Function Example
Pricing
Pay per usage
Go to Apify Store
Rust Input Function Example
Dynamically compile and run input-provided page function. Like Cheerio Scraper but in Rust.
0.0 (0)
Pricing
Pay per usage
2
3
1
Last modified
a year ago
Example actor showcasing running a user-provided function in a static-typed compiled language.
How does it work
- Reads the input from disk or via Apify API
 - Extracts the 
page_functionstring from the input - Stores the 
page_functionstring to the disk - Spawns a system process using 
cargoto compile thepage_functioninto a dynamic library - Dynamically links the library and converts the 
page_functioninto a regular Rust function. It must adhere to predefined input/output types. - The example code gets HTML from the input provided 
urland parses it into adocumentusing the Scraper library - The user-provided 
page_functiongets thedocumentas an input parameter and returns a JSON Value type using thejsonmacro 
Page function
Page function can use a predefined set of Rust libraries, currently only the Scraper library and serde_json for JSON Value type are provided.
TODO
But technically, thanks to dynamic compiling, we can enable users to provide a list of libraries to be used in the page_function.
Example page_function
use serde_json::{Value,json};use scraper::{Html, Selector};fn selector_to_text(document: &Html, selector: &str) -> Option<String> {document.select(&Selector::parse(selector).unwrap()).next().map(|el| el.text().next().unwrap().into() )}#[no_mangle]pub fn page_function (document: &Html) -> Value {println!("page_function starting");let title = selector_to_text(&document, "title");println!("extracted title: {:?}", title);let header = selector_to_text(&document, "h1");println!("extracted header: {:?}", header);let companies_using_apify = document.select(&Selector::parse(".Logos__container").unwrap()).next().unwrap().select(&Selector::parse("img").unwrap()).map(|el| el.value().attr("alt").unwrap().to_string()).collect::<Vec<String>>();println!("extracted companies_using_apify: {:?}", companies_using_apify);let output = json!({"title": title,"header": header,"companies_using_apify": companies_using_apify,});println!("inside pageFunction output: {:?}", output);output}



