Write a Rust program to render a tweet given the text and an unordered list of its entities (hashtags, URLs, @mentions). Each entity is represented by the replacement text, a start index and an end index.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Rust program that renders a tweet given the text and an unordered list of its entities (hashtags, URLs, @mentions):
rust
use std::cmp::Ordering;
struct Entity {
replacement: String,
start: usize,
end: usize,
}
impl PartialEq for Entity {
fn eq(&self, other: &Self) -> bool {
self.start == other.start
}
}
impl Eq for Entity {}
impl PartialOrd for Entity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.start.cmp(&other.start))
}
}
impl Ord for Entity {
fn cmp(&self, other: &Self) -> Ordering {
self.start.cmp(&other.start)
}
}
fn render_tweet(text: &str, entities: &mut Vec<Entity>) -> String {
entities.sort();
let mut rendered_tweet = String::new();
let mut last_end = 0;
for entity in entities {
if entity.start > last_end {
rendered_tweet.push_str(&text[last_end..entity.start]);
}
rendered_tweet.push_str(&entity.replacement);
last_end = entity.end;
}
if last_end < text.len() {
rendered_tweet.push_str(&text[last_end..]);
}
rendered_tweet
}
fn main() {
let tweet_text = "Hello, @john! Check out this link: https://example.com #rustlang";
let mut entities = vec![
Entity {
replacement: "<a href='https://twitter.com/john'>@john</a>".to_string(),
start: 7,
end: 12,
},
Entity {
replacement: "<a href='https://example.com'>https://example.com</a>".to_string(),
start: 31,
end: 49,
},
Entity {
replacement: "<a href='https://twitter.com/hashtag/rustlang'>#rustlang</a>".to_string(),
start: 50,
end: 59,
},
];
let rendered_tweet = render_tweet(tweet_text, &mut entities);
println!("{}", rendered_tweet);
}
In this program:
We define an Entity struct that represents an entity in the tweet. It contains the replacement text, start index, and end index.
We implement the PartialEq, Eq, PartialOrd, and Ord traits for the Entity struct to enable sorting based on the start index.
The render_tweet function takes the tweet text and a mutable reference to a vector of entities. It sorts the entities based on their start index.
We iterate over the sorted entities and construct the rendered tweet by appending the text between entities and the replacement text for each entity.
If there is any remaining text after the last entity, we append it to the rendered tweet.
In the main function, we define the tweet text and a vector of entities. Each entity contains the replacement text (e.g., HTML anchor tags) and the start and end indices.
We call the render_tweet function with the tweet text and the entities, and store the result in the rendered_tweet variable.
Finally, we print the rendered tweet.
When you run this program, it will output:
code
Hello, <a href='https://twitter.com/john'>@john</a>! Check out this link: <a href='https://example.com'>https://example.com</a> <a href='https://twitter.com/hashtag/rustlang'>#rustlang</a>
The rendered tweet replaces the entities with their corresponding replacement text based on the provided start and end indices.