Welcome! This is my personal blog about Web technologies, software development, open source and other related topics
The ideas and opinions expressed here are solely mine and don't represent those of others, either individuals or companies.The code snippets or references to software products or analogous are to be used without any warranty of any kind. If you enjoy the content, feel free to share it and re-use it as long as you provide a link to the original post.
GraphQL delivery API is used to access the published site content. Experience Edge for Experience Manager (XM) is an API-based service from Sitecore that gives you globally replicated, scalable access to your XM Cloud items, layout, and media. You can use the standard publish tools in XM Cloud , but instead of rendering content from a self-hosted Content Delivery environment, Experience Edge provides you a Sitecore-hosted GraphQL API.
To access the XM Cloud hosted published site content using Expereince Edge follow these steps-
Login to XM Cloud. Navigate to Projects ==> Environments ==>Details tab.
impl Description for Language{
fn represent(&self) -> String{
todo!();
//format!("the language {} was lauched in {} year and current stable version is {}", self.name, self.launched_year, self.stable_version)
}
}
Output
Rust
todo!() is actually the same as another macro: unimplemented!(). Programmers were using unimplemented!() a lot but it was long to type, so they created todo!() which is shorter.
No dummy variable needed
Visual “not complete” indicator
Will panic if reached
Panic if reached – if we call the unimplemented trait it will panic and give error.
This macro is kind of like todo!() except it’s for code that you will never do. Maybe you have a match in an enum that you know will never choose one of the arms, so the code can never be reached. If that’s so, you can write unreachable!() so the compiler knows that it can ignore that part.
unreachable!() is also nice for you to read because it reminds you that some part of the code is unreachable. You have to be sure that the code is actually unreachable though. If the compiler ever calls unreachable!(), the program will panic.
dbg! macro
dbg! is a very useful macro that prints quick information. It is a good alternative to println! because it is faster to type and gives more information
As per the Rust documentation, the dbg! macro works exactly the same in release builds. This is useful when debugging issues that only occur in release builds or when debugging in release mode is significantly faster.
[src\main.rs:33:5] &pro_langauge_rust = Language {
name: "Rust",
stable_version: 1.84,
launched_year: 2012,
}
[src\main.rs:34:5] pro_langauge_rust.represent() = "the language Rust was lauched in 2012 year and current stable version is 1.84"
env! and option_env! macro
Any sensitive information that are harcoded can be a security breach. To avoid this these should be go set as environment variables. For this use env! or option_env!
env!(“key”) or option_env!(“key”)
env! will panic if the key does not exists. It is better to use env! if you want program to crash when the environment variable is not found.
option_env! – on the other hand is safer as it won’t panic if the key not found, instead allows to handle the error.
let language_name = env!("name");
This will not compile if the environment variable is not set in project. To do this you can use dotenc crate to read variables from .env file.
But what when the variables are set through command line. This won’t even compile to run the project. Use option_env! macro
let language_name = option_env!("name").expect("Language name is required!");
Make Struct Field optional using Option Enum
Use Option to make the struct field optional and set it to None if it has to be empty or Some if the value has to be set.
Release profile – runs fast but compiles slow as it executes all of the compile optimisations.
cargom run --release
fold() method
fold is a consuming iterator adaptor which applies a function to each element of the iteration, accumulating the result into a new value.
This method is used a lot to add together the items in an iterator, but you can also do a lot more. It is somewhat similar to .for_each(). In .fold(), you first add a starting value (if you are adding items together, then 0), then a comma, then the closure. The closure gives you two items: the total so far, and the next item. First here is a simple example showing .fold() to add items together.